Displaying content in home page in django - python

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'))

Related

django 2.0.7 NoReverseMatch at /posts

Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name.
After Django goes to 2.0.7, I set up some url rules. It works locally at 127.0.0.1 on my computer, but not remotely on the machine (ubuntu 1604).
code:
post_list.html:
{% for post in posts %}
<div>
<p>published: {{ post.published_date }}</p>
<h1>{{ post.title }}</h1>
<p>{{ post.text|linebreaksbr }}</p>
</div>
{% endfor %}
blog.urls:
urlpatterns = [
path('', views.home_page, name='home_page'),
path('posts', views.post_list, name='post_list'),
path('post/<pk>/', views.post_detail, name='post_detail'),
]
blog.views:
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
mysite.urls:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
Solved. I need to add 'blog' as my app_name in blog.url and add 'blog' as my namespace in mysite.url
Thanks guy!
In 2.0.7, I need to add an app_name (blog) in blog.url and add a namespace in mysite.url (blog). Only adding a namespace in mysite.url without adding an app_name in blog.url -> django will tell me to add an app name. Only adding an app_name in blog.url without adding a namespace in mysite.url -> django will tell me blog is a not registered namespace. Also remember to refresh gunicorn.
Suppose your app name is blog so in your blog's urls.py add
app_name= 'blog'
and in template use {% url 'blog:post_detail' pk=post.pk %}

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

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 %}

Unexpected NoReverseMatch error when using include() in urls patterns

I'm getting an error when referencing detail.html in index.html
Reverse for 'detail' with arguments '(3,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['$(?P<pk>[0-9]+)/$']
views.py
def rock_and_feat(request):
feats = Feat.objects.order_by('-created')[:3]
rocks = Rockinfo.objects.order_by('-rank')[:50]
context = RequestContext(request, {
'feats': feats, 'rocks': rocks
})
return render_to_response('template.html', context)
class DetailView(generic.DetailView):
model = Feat
template_name = 'feature/detail.html'
context_object_name = 'feat'
urls.py
urlpatterns = [
url(r'^$', views.rock_and_feat, name='rock_and_feat'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
]
index.html
{% extends "index.html" %}
{% block mainmast %}
<div id="wrapper">
{% if feats %}
{% for feat in feats %}
<div class="specialsticky">
<img src="{{ feat.image.url }}" alt="some text">
<h1 class="mast-header">
{{feat.title}}
</h1>
</div>
{% endfor %}
{% else %}
<p>No </p>
{% endif %}
</div>
{% endblock %}
detail.html
{% extends "index.html" %}
<iframe width="560" height="345" src="{{ feat.youtube_link }}" frameborder="0" allowfullscreen></iframe>
project urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^$', include('feature.urls', namespace="feature")),
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
the app worked fine before I added the <a href= on the image in index.html.
Can't figure out what's wrong.
This indicates the problem.
'$(?P<pk>[0-9]+)/$'
There shouldn't be a dollar sign (which matches the end of the string) at the beginning of the pattern.
The problem is caused by the way you are including the urls.py. You currently have a dollar in the regex:
url(r'^$', include('feature.urls', namespace="feature")),
To fix the problem, remove the dollar from the regex.
url(r'^', include('feature.urls', namespace="feature")),

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