Reputation: 163
I am trying to get my index page to open on my Django 2 project, but I cant seem to get it to open a simple page from the templates in my music app(index.html). Using class based views at the moment.
My releases URL works but that is using he re_path. I cant seem to get the normal path to work at all.
I haven't looked at the project for a year and am not normally working with Django, but want to get back into it.
urls.py
from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# include urls from the music app
path('music/', include('music.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
music/urls.py
from django.contrib import admin
from django.urls import path, include, re_path
from . import views
# defined the app name in case the same fields are used in other apps
app_name = 'music'
urlpatterns = [
# no info past music return index EG /music/
path('', views.IndexView.as_view(), name='index'),
re_path(r'^release/$', views.ReleaseView.as_view(), name='release'),
re_path(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name="detail"),
re_path(r'^(?P<pk>[0-9]+)/$', views.ArtistView.as_view(), name="artist"),
re_path(r'^register/$', views.UserFormView.as_view(), name="register"),
]
views.py
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.views import generic
from django.views.generic import ListView, View
from .models import Artist, Track, Release
from .forms import UserForm
from urllib import request
class IndexView(View):
template_name = 'music/index.html'
# def get(self, request):
# return render(request, "music/index.html")
class ReleaseView(generic.ListView):
template_name = 'music/releaselist.html'
context_object_name = 'all_releases'
def get_queryset(self):
return Release.objects.all()
class ArtistView(generic.ListView):
model = Artist
template_name = 'music/base.html'
context_object_name = 'all_artists'
def get_queryset(self):
artist_name = Artist.objects.all()
return render(request, "music/base.html", {'artist_name': artist_name})
# return render(request, "music/base.html", {'artists': artists})
class DetailView(generic.DetailView):
model = Release
template_name = 'music/detail.html'
class UserFormView(View):
form_class = UserForm
template_name = 'music/registration_form.html'
# blank form
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form': form})
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
user = form.save(commit=False)
# cleaned normalised data
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
user.save()
# returns user objects if credential are valid
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return redirect('music:index')
return render(request, self.template_name, {'form': form})
My base.html and index.html are located in recordlabel/music/templates
If anyone can spot anything that would be a great help as I know its something stupid that im not doing. I have tried makemigrations, and migrate and the site loads fine with the releases page and the data and images all loading fine. Just cant get the index page tot work and it never worked before.
Feel free to ping me for any additional files, but I think its somewhere in these files. I have looked at the documentation at the top of the file and also in the official documentation and couldn't get anything to work from there also.
Upvotes: 0
Views: 331
Reputation: 2383
You registered music/
but not music/index/
. The name=
reference is only used when you need to call a soft-coded URL in your template through something like {% url 'music:index' %}
.
Just FYI, 127.0.0.1:8000/music
and 127.0.0.1:8000/music/
are different routes.
EDIT
I know this is not a part of your question, but this part
re_path(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name="detail"),
re_path(r'^(?P<pk>[0-9]+)/$', views.ArtistView.as_view(), name="artist"),
may be prone to bugs. What if you have an artist and a song with the same name? Than Django will have conflicting URLs and often it's annoying to deal with. Just append prefixes like (respectively) song/
or detail/
before the primary keys.
Upvotes: 1