Alternative/ How to use {% url %} in django 1.8? - python

I'm learning django on version 1.8.
In their documentation they suggest to use {% url %} template tag in order to avoid hardcoding the url. But it is not working in the v1.8, and confirmed that it is deprecated in this version.
https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#url
Does anyone know any alternative ?
Update:
p_index.html
{% if latest_question_list %}
<ul>
{% for que in latest_question_list %}
<li>{{ que.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p> No polls questions are availabe. </p>
{% endif %}
Above code gives me this error: 'str' object has no attribute 'regex'
When I change the href line as below, works fine!
<li>{{ que.question_text }}</li>
urlpatterns in polls app:
urlpatterns = [
url(r'^$', views.index, name='view_index'),
# ex: /polls/5/
url(r'^(?P<q_no>[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<q_no>[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<q_no>[0-9]+)/vote/$', views.vote, name='vote'),
]
The main urls.py is as below :
urlpatterns = [
url(r'^$', 'home.views.index'),
url(r'^polls/', include('polls.urls', namespace = 'polls')),
url(r'^android/start', 'testpage.views.androidStart'),
url(r'^admin/', include(admin.site.urls)),
]

I think you got this wrong. The {% url %} tag is not depercated to my best of knowledge, and I have never heard of any alternative. What is deprecated is the syntax to pass a dotted Python path:
Deprecated since version 1.8:
You can also pass a dotted Python path to a view function, but this syntax is deprecated and will be removed in Django 1.10:
{% url 'path.to.some_view' v1 v2 %}

Related

Error - (django-taggit) - 'post_list_by_tag' with arguments '('',)' not found

studying django, can't find a solution. added tags and the site gives an error
studying django, can't find a solution. added tags and the site gives an error
list.html
<p class="tags">
Tags:
{% for tag in post.tags.all %}
<a href="{% url 'blog:post_list_by_tag' tag.slug %}">
{{ tag.name }}
</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
</p>
```
urls.py
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
# post views
path('', views.post_list, name='post_list'),
# path('', views.PostListView.as_view(), name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/',views.post_detail,name='post_detail'),
path('<int:post_id>/share/', views.post_share, name='post_share'),
path('tag/<slug:tag_slug>/', views.post_list, name='post_list_by_tag'),
]
solved the problem by deleting all previously created posts without tags

Why does the hard-coded url work but not the name-spaced url in Django?

I need someone that really knows their Django. So I can always give more detail if it's necessary but long story short, I'm trying to link to urls using django's url tag and I get a NoReverseMatch exception everytime.
<ul>
{% for item in results %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
BUT, when I use the hardcoded url, this works perfectly fine:
<ul>
{% for item in results %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
This is what the url.py module looks like:
from django.urls import path
from . import views
app_name = 'cards'
urlpatterns = [
path('', views.index, name='index'),
path('search/', views.search, name='search'),
path('search-results/<keywords>/', views.search_results, name='search-results'),
path('stats/<item_title>/', views.stats, name='stats')
]
Does anyone have any idea why this might be happening? Let me know if you need more details. Any help is appreciated, thanks!

Displaying content in home page in django

I am following the Django tutorial. I have a /films page and /films/id page displaying successfully. I would like to divert slightly from the tutorial and one step of this is to display the content which is currently appearing in /films in the website home e.g. http://127.0.0.1:8000/
I currently have:
index.py:
def index(request):
latest_film_list = Film.objects.order_by('-pub_date')[:5]
context = {'latest_film_list': latest_film_list}
return render(request, 'films/index.html', context)
index.html:
{% if latest_film_list %}
<ul>
{% for film in latest_film_list %}
<li>{{ film.title }}</li>
{% endfor %}
</ul>
{% else %}
<p>No films are available.</p>
{% endif %}
films/urls.py:
urlpatterns = [
path('', views.index, name='index'),
# # ex: /films/5/
path('<int:film_id>/', views.detail, name='detail'),
# # ex: /films/5/results/
path('<int:film_id>/results/', views.results, name='results'),
# # ex: /films/5/vote/
path('<int:film_id>/vote/', views.vote, name='vote'),
]
mysite.urls.py
urlpatterns = [
url(r'^films/', include('films.urls')),
url(r'^admin/', admin.site.urls),
]
I realise I probably just need to add/adjust the films/urls.py file but have been unsuccessful so far. Home currently displays a 404.
You can simply change your url as follows:
From this:
url(r'^films/', include('films.urls'))
To this:
url(r'^', include('films.urls'))

404 error when viewing details of a poll in Django

I'm working through the Django tutorial anf. For some reason, if I try to manually type in the URL like so: http://localhost:8000/polls/1/ I can view the poll (in this case the id of the poll is 1). But if I'm at the index of all the polls, which would be the URL: http://localhost:8000/polls and I try to click on one of the polls, it takes me to an error page with the following:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/ ^$ [name='index']
^polls/ ^(?P<pk>\d+)/$ [name='detail']
^polls/ ^(?P<pk>\d+)/results/$ [name='results']
^polls/ ^(?P<poll_id>\d+)/vote/$ [name='vote']
^admin/
The current URL, polls/{% url 'polls:detail' poll.id % }, didn't match any of these.
Here's my polls/urls file:
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
And my mysite/urls file:
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace='polls')),
url(r'^admin/', include(admin.site.urls)),
)
And my template file:
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
I've seen similar questions but it doesn't seem that they apply to me. Why does it work when I type in the URL but not when I click on the link that should take me to the same place?
You have space between % and }, try this
<a href="{% url 'polls:detail' poll.id %}">

Django "remove hardcoded urls in templates" not working

I'm checking out django for the first time following the tutorial https://docs.djangoproject.com/en/1.6/intro/tutorial03/
So far it's been plain sailing. I've hit a snag at the Remove hard coded urls section. I'm using Django 1.6.6.
When I change the hardcoded url from:
<li>{{ poll.question }}</li>
to:
<li>{{ poll.question }}</li>
I get a 404 error as follows:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/polls/%7B%%20url%20'detail'%20poll.id%20%7D
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/ ^$ [name='index']
^polls/ ^(?P<poll_id>\d+)/$ [name='detail']
^polls/ ^(?P<poll_id>\d+)/results/$ [name='results']
^polls/ ^(?P<poll_id>\d+)/vote/$ [name='vote']
^admin/
The current URL, polls/{% url 'detail' poll.id }, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
The url that shows up in the address bar is
mylocalenv/polls/%7B%%20url%20%27detail%27%20poll.id%20%7D
I've tried removing the quotes from 'detail' but given I'm on django 1.6.6 I shouldn't need to. It didn't work either. I've also tried skipping ahead a bit and including the /polls/ namespace in urls.py but again, no joy.
My urls.py file looks like this:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
# ex: /polls/5/results/
url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
# ex: /polls/5/vote/
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
The exact code in my index.html template is:
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
Got it
But frustratingly I have no idea why. Out of desperation in my index.html I started playing around with the index.html file on this line:
<li>{{ poll.question }}</li>
For no logical reason I changed poll.id to poll_id. Reloading broke index.html as I expected. When I changed it back to poll.id everything works. This makes no sense. I even ctrl^z back to the index.html that previously wasn't working and now that's working too. I restarted the server before each check. I'm annoyed. But at least it's working.
This line in the trace back suggests you have missed out the % before the closing curly brace.
The current URL, polls/{% url 'detail' poll.id }, didn't match any of these.
Even, I was facing this problem and for me the reason was that I gave space between { % and % }, where as the correct syntax is {% and %}. For completeness I have given the correct and incorrect syntax below -
correct syntax - <li>{{ poll.question }}</li>
incorrect syntax - <li>{{ poll.question }}</li>
check you polls/urls.py file, make sure your urlpatterns is [], not {}
urlpatterns = [
path('', views.index, name='index'),
]
I just restarted the server and interestingly it just worked.

Categories