Search code examples
pythondjangodjango-rest-frameworkslug

Django how to use the slug in URL as a variable


I want to use the slug in the URL as a variable for my API. So first I need to me to put the slug in the urls.py which looks something like this.

url(r'^use/(?P<slug>[\w-]+)/$', view_function, name="api")

Then I need to includ the slug in the view.py, which look something like this:

def urlextractor(request, slug):
    if request.method == 'POST':
        serializer = Serializer(data=slug)
        # do something with slug

The problem that is that I get the code 404. Can you give me some example or references that I can look at?


Solution

  • The question was already answered by a user who's comment was deleted. He said that in order for my module to take the URL as a variable I only need to change the urls.py into a regular expression:

    url(r'^use/(?P[A-Za-z]+)/$', view_function, name="api")