[Answered ]-Django url pattern issue in python 2.7.6

2👍

There are a few issues with your setup; but nothing that’s major:

  1. Your file is called haliyikama/first/url.py; but you are including it as url(r'^', include('first.urls')), (note the urls); you need to change the name of your file to haliyikama/first/urls.py.

  2. Your patterns are also incorrect. Your pattern needs to be ^$ for the very first pattern in your url list.

  3. The reason admin is not working, is because you haven’t mapped it to any url pattern; try adding url(r'^admin/', admin.site.urls), to your main urls.py.

  4. href="{% url home.html %}" this is not how you use the url tag, you need to pass it the name of the view, not the template; like this href="{% url 'home' %}".

I also suggest you read a quick primer on regular expressions because these are important when defining urls; as they work on the concept of pattern matching.

Here are some quick tips:

Lets say you have a pattern like this url(r'^home/')

This means any url that starts with home/, so it will match home/foo/bar/zoo/hello along with anything else home/hello.html etc. This is why usually when you have an “open ended” pattern like this, you usually include a lot of other urls after it, like this url(r'^home/', include('home.urls')).

Now, when you do url(r'^home/', include('home.urls')) this means “any url that starts with home/, match it against the list of urls found in home/urls.py file”.

Lets say in our home/urls.py file, we have:

url(r'^$', views.index),

This ^$ means a blank string, so views.index will be called when the url is http://localhost:8080/home/

Here is another example:

url(r'^members/$', views.members),`

Now this will match home/members/.

Hopefully this helps clarify the issues you are facing. Please have a read through the tutorial which covers these concepts.

0👍

The main problem I had was using the URL tags. If you had some problem like this and pop-up in here, here is the answer how I solve my problem with the help I get from Burhan Khalid;

Yes my url tag was wrong but instead of href=”{% url home %}” doing something like that I should have do something like href=”{% url ‘home’ %}”
there is not much difference but it will help to call the name so it can send you that link. The Url tags in the HTML file was wrong.

Leave a comment