Getting 404 error on signup and some more URLs. /login, /logout, /admin is working perfectly. I'm making a web app that lets a user login, logout, search a flight, book a flight and view the bookings made.
urls.py
from django.urls import path
from . import viewsurlpatterns = [path('', views.home, name='home'),path('search_flights/', views.search_flights, name='search_flights'),path('book_flight/<int:flight_id>/', views.book_flight, name='book_flight'),path('my_bookings/', views.my_bookings, name='my_bookings'),path('login/', views.user_signup, name='login'),path('signup/', views.user_signup, name='signup'),]
views.py
from django.shortcuts import render, redirect
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from .models import Flight, Booking
from django.contrib.auth.forms import UserCreationForm
from .forms import SignUpForm, SignInForm, EditProfileFormdef home(request):flights = Flight.objects.all()return render(request, 'home.html', {'flights': flights})@login_required
def search_flights(request):if request.method == 'POST':date = request.POST.get('date')time = request.POST.get('time')flights = Flight.objects.filter(date=date, time=time)return render(request, 'search_flights.html', {'flights': flights})return render(request, 'search_flights.html')@login_required
def book_flight(request, flight_id):flight = Flight.objects.get(id=flight_id)if request.method == 'POST':# Check if seats are availableif flight.seats_available() > 0:# Book the ticketbooking = Booking.objects.create(flight=flight, user=request.user)return redirect('my_bookings')else:# Show error messagereturn render(request, 'book_flight.html', {'flight': flight, 'error': 'No seats available.'})return render(request, 'book_flight.html', {'flight': flight})@login_required
def my_bookings(request):bookings = Booking.objects.filter(user=request.user)return render(request, 'my_bookings.html', {'bookings': bookings})def user_login(request):if request.method == 'POST':form = AuthenticationForm(request, data=request.POST)if form.is_valid():username = form.cleaned_data.get('username')password = form.cleaned_data.get('password')user = authenticate(username=username, password=password)if user is not None:login(request, user)return redirect('home')else:form = AuthenticationForm()return render(request, 'login.html', {'form': form})def user_signup(request):if request.method == 'POST':form = UserCreationForm(request.POST)if form.is_valid():user = form.save()login(request, user)return redirect('home')else:form = UserCreationForm()return render(request, 'signup.html', {'form': form})
Any idea what might be going wrong? I am skeptical about the placement of the templates folder, but changing that introduced the Template doesn't exist error.
main urls.py
from django.urls import path, include
from . import views
from django.contrib import admin
from django.views.generic.base import TemplateViewurlpatterns = [path(r'admin/',admin.site.urls),path('', views.home, name='home'),path('app/', include('app.urls')),path('',include('django.contrib.auth.urls')),path('',TemplateView.as_view(template_name='home.html'),name='home')]
Directory structure-
flight
└─ flight_booking├─ app│ ├─ admin.py│ ├─ apps.py│ ├─ migrations│ │ ├─ 0001_initial.py│ │ ├─ __init__.py│ │ └─ __pycache__│ │ ├─ 0001_initial.cpython-39.pyc│ │ └─ __init__.cpython-39.pyc│ ├─ models.py│ ├─ templates│ │ └─ app│ │ ├─ book_flight.html│ │ └─ search_fights.html│ ├─ tests.py│ ├─ urls.py│ ├─ views.py│ ├─ __init__.py│ └─ __pycache__│ ├─ admin.cpython-39.pyc│ ├─ apps.cpython-39.pyc│ ├─ forms.cpython-39.pyc│ ├─ models.cpython-39.pyc│ ├─ urls.cpython-39.pyc│ ├─ views.cpython-39.pyc│ └─ __init__.cpython-39.pyc├─ db.sqlite3├─ flight_booking│ ├─ asgi.py│ ├─ settings.py│ ├─ urls.py│ ├─ views.py│ ├─ wsgi.py│ ├─ __init__.py│ └─ __pycache__│ ├─ settings.cpython-39.pyc│ ├─ urls.cpython-39.pyc│ ├─ views.cpython-39.pyc│ ├─ wsgi.cpython-39.pyc│ └─ __init__.cpython-39.pyc├─ manage.py└─ templates├─ base.html├─ home.html└─ registration├─ login.html└─ signup.html