Basically, my project has three apps: 1.logreg (for login/register) 2.usersite 3.adminsite Since, I am a beginner, I am just testing the code for the admission table only which is in usersite app. Admission Table When I try to go to the next page after filling the details of admission table through the form. I am getting an error like this:
Using the URLconf defined in proctor.urls, Django tried these URL patterns, in this order:
[name='login']
login [name='login']
registration [name='registration']
logout [name='logout']
usersite
adminsite
admin
The current path, submit_admission/, didn’t match any of these.
This is the urls.py of my project proctor
from xml.etree.ElementInclude import include
from django.contrib import admin
from django.urls import path,include
#from usersite.views import admission,submit_admission
urlpatterns = [
path('',include('logreg.urls')),
path('usersite',include('usersite.urls')),
path('adminsite',include('adminsite.urls')),
path('admin', admin.site.urls),
#path('submit_admission/', submit_admission, name='submit_admission'),
]
This is the urls.py of the usersite app
from django.urls import path
from.import views
from django.contrib.auth import views as auth_views
from usersite.views import submit_admission
urlpatterns = [
#path('', views.admission, name='admission'),
path('', views.admission, name='admission'),
path('submit_admission/', submit_admission, name='submit_admission'),
path('academicdetails', views.academic, name='academic'),
path('achievementdetails', views.achievements, name='achievements'),
path('personalinfodetails', views.personalinfo, name='personalinfo'),
path('unauth', views.unauthorised, name='unauth'),
path('logout', views.logout_view, name='logout'),
path('logreg/login/', auth_views.LoginView.as_view()),
]
This is the views.py of the usersite app
Ignore the code after def submit_admission(request):
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth import logout
from django.contrib.auth.models import User
from django.conf import settings
from django.shortcuts import redirect
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from usersite.forms import admission1
@login_required(login_url='logreg/login')
@login_required
def admission(request):
if not request.user.is_authenticated:
return redirect('unauthorised')
else:
form = admission1()
return render(request,'admission.html', context={'form': form})
@login_required
def submit_admission(request):
if request.user.is_authenticated:
user = request.user
print(user)
form = admission1(request.POST)
if form.is_valid():
print(form.cleaned_data)
return redirect("admission")
else:
return render(request,'admission.html',context = {'form': form})
@login_required
def academic(request):
if not request.user.is_authenticated:
return redirect('unauthorised')
else:
return render(request,'academic.html')
@login_required
def achievements(request):
if not request.user.is_authenticated:
return redirect('unauthorised')
else:
return render(request,'achievements.html')
@login_required
def personalinfo(request):
if not request.user.is_authenticated:
return redirect('unauthorised')
else:
return render(request,'personalinfo.html')
@login_required
def logout_view(request):
logout(request)
return render(request,'logout.html')
def unauthorised(request):
return render(request,'unauthorised.html')
I have the app written in INSTALLED_APPS which is in settings.py of project
this is installed apps
INSTALLED_APPS = [
# logreg app
'logreg.apps.LogregConfig',
# crispy forms
'crispy_forms',
'adminsite.apps.AdminsiteConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'usersite',
]
This is the models.py of usersite app
from django.db import models
from django.contrib.auth.models import User
class admission1(models.Model):
year = models.CharField(max_length=45, blank=True, null=True)
category = models.CharField(max_length=45, blank=True, null=True)
hsc = models.CharField(max_length=45, blank=True, null=True)
cet = models.CharField(max_length=45, blank=True, null=True)
jee = models.CharField(max_length=45, blank=True, null=True)
diploma = models.CharField(max_length=45, blank=True, null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
This is the forms.py of usersite app
from django.forms import ModelForm
from usersite.models import admission1
class admission1(ModelForm):
class Meta:
model = admission1
fields = ['year','category','hsc','cet','jee','diploma']
I have the form tag in admission.html just like this
Not giving full html code to use less space
<form action="/submit_admission/" method="post">
{% csrf_token %}
<h4 style="padding-top: 20px; text-align: center; text-transform:uppercase;"> Department of</h4>
</form>
I tried adding the submit_admission url in the urls.py of project(proctor) and fortunately it worked. But this is not the right way I guess. The url of each action should be in their respective apps urls.py which is the best practice. Can anyone please help me with this issue. I want the action of submit_admission mapped in the urls.py of usersite app. I don't know what mistake I am making.
CodePudding user response:
Since you used a root url config where you add a path prefix to each different app (eg usersite, adminsite) and the part that you need, submit-admission
can be found in the usersite part of your project, the url /submit_admission/
does not exist. You will need /usersite/submit_admission
.
In Django, the most common way to do this is to let Django create the urls itself. You can just add the name in there like this:
<form action="{% url 'submit_admission' %}" method="post">
{% csrf_token %}
<h4 style="padding-top: 20px; text-align: center; text-transform:uppercase;"> Department of</h4>
</form>
Maybe it can be like this: {% url 'usersite:submit_admission' %}
.