简体   繁体   中英

Dynamic Url's in Django

So I have a list of items and I want to have all of the items direct the user to a standard info page about that item when clicked. I don't full understand the dynamic url system in django. My html file for the content is called detail.html. The goal is to click a movie item, and be directed to a page title "blahblah.com/specific_movie_title"

In views.py I have:

def detail(reuqest, movie_title):
    moviePage = Movie.objects.get(pk=movie_title)
    return render_to_response('detail.html', {'detail':moviePage, RequestContext(request))

In the urls.py the line I have to correspond with this is:

url(r'^$', views.detail, name='detail')

Could I get help with correcting these two chunks of code?

In django, urls are matched by a regular expression pattern.

The general format of the url line is:

url(the_pattern, the_view_name_or_callable, **any_extra_arguments)

The key point is the_pattern , which is a regular expression and whatever is matched can be passed as an argument to the view function.

It is important that any parts of the pattern that are captured and passed on to the view function, actually match the function's signature (the def line, where you define the name and arguments). Otherwise, django will throw an error.

So now, with that out of the way - lets deal with the actual issue.

Your want a url like /the_great_gatsby to redirect to the page for The Great Gatsby .

The first step is to identify a pattern that matches the_great_gatsby . You can use [_\\w]+ which means "one or more word characters or _", and plug it into the URL:

url(r'/[_\w]+$', views.detail, name='detail')

Next, you have to tell django how to capture that pattern and pass it to the view method as an argument.

Your view method is: def detail(request, movie_title) . So django must pass whatever is after the / in the url (and matches the pattern) to the argument name movie_title , so what we want is this:

def detail(request, movie_title)
                    ^^^^^^^^^^^
          ---------------|
          |        
       vvvvvvv
url(r'/[_\w]+$', views.detail, name='detail')

We modify the regular expression to make sure that django captures whatever matches, and then assigns it a name. To do that, wrap the part of the regular expression in (?P<name_of_variable>expression) like this:

url(r'/(?P<movie_title>[_\w+])$', views.detail, name='detail')

Now, whatever is captured after / will be passed as the argument movie_title to the views.detail method.


Of course, you have to make sure your view function is doing the right thing with the captured string. In your case, your view method is searching by the primary key and will fail to produce any results (and will raise an exception) since there is no movie that will have the title as the primary key; but that's another problem.

you can configure your url and view like this

urls.py

url(r'^movie/(?P<movie_title>\w+)$', views.movie_detail, name='detail')

views.py

def movie_detail(request, movie_title):
    movie_object = Movie.objects.get(pk=movie_title)
    return render(request, 'moview-detail.html', {'movie_object':movie_object}

and in your html {% url 'app_name:detail' movie_title %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM