Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django URL.py and the index

I want to know what is the best way to write in the URL.py. I am trying to get the index in this way: www.example.com with (r'',index). But when I try r'', all pages in the website are going to the home page.

Part of my url.py:

(r'^index',homepages),
(r'',homepages),

Thanks :)

like image 494
Asinox Avatar asked Aug 22 '09 18:08

Asinox


People also ask

How do I reference a URL in Django?

The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py . Listing 2-16 shows how to name a project's home page, as well as how to reference this url from a view method or template.

How does urls py work in Django?

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info . Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view).

What is the difference between path and URL in Django?

In Django 2.0, you use the path() method with path converters to capture URL parameters. path() always matches the complete path, so path('account/login/') is equivalent to url('^account/login/$') . The part in angle brackets ( <int:post_id> ) captures a URL parameter that is passed to a view.


2 Answers

Like this:

 #...
 (r'^$', index),
 #...
like image 180
Brian R. Bondy Avatar answered Sep 19 '22 09:09

Brian R. Bondy


Django URL matching is very powerful if not always as convenient as it could be. As Brian says, you need to use the pattern r'^$' to force your pattern to match the entire string. With r'', you are looking for an empty string anywhere in the URL, which is true for every URL.

Django URL patterns nearly always start with ^ and end with $. You could in theory do some fancy URL matching where strings found anywhere in the URL determined what view function to call, but it's hard to imagine a scenario.

like image 44
Ned Batchelder Avatar answered Sep 19 '22 09:09

Ned Batchelder