Dan
Dan

Reputation: 946

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><a href="/films/{{ film.id }}/">{{ film.title }}</a></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.

Upvotes: 0

Views: 39

Answers (1)

Alessio Ferri
Alessio Ferri

Reputation: 345

You can simply change your url as follows:

From this:

url(r'^films/', include('films.urls'))

To this:

url(r'^', include('films.urls')) 

Upvotes: 1

Related Questions