[英]Python Django Error during template rendering
let me quickly explain what am I trying to do.让我快速解释一下我要做什么。
So I am making a small Django based Conference Management, where there can be multiple conferences, a single conference can have multiple talks and each talk will have a certain number of Speakers and Participants.所以我正在制作一个基于会议管理的小型 Django ,其中可以有多个会议,一个会议可以有多个演讲,每个演讲都有一定数量的演讲者和参与者。
I am able to list the conferences and give the option to delete and edit a conference.我能够列出会议并提供删除和编辑会议的选项。
Problem:问题:
I want an option where I click on a conference and it shows a list of talks for that conference.我想要一个选项,点击一个会议,它会显示该会议的演讲列表。 but when I click on the conference it shows the following error.
但是当我点击会议时,它显示以下错误。
NoReverseMatch at /Big Data/talks/
Reverse for 'conferenceedit' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<id>[^/]+)/edit$']
Request Method: GET
Request URL: http://127.0.0.1:8000/Big%20Data/talks/
Django Version: 3.2.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'conferenceedit' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<id>[^/]+)/edit$']
Exception Location: E:\python\PYTHON and GIT\lib\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix
Python Executable: E:\python\PYTHON and GIT\python.exe
Python Version: 3.9.1
Python Path:
['D:\\DjangoConferenceManagementSystem\\mysite',
'E:\\python\\PYTHON and GIT\\python39.zip',
'E:\\python\\PYTHON and GIT\\DLLs',
'E:\\python\\PYTHON and GIT\\lib',
'E:\\python\\PYTHON and GIT',
'C:\\Users\\AKS\\AppData\\Roaming\\Python\\Python39\\site-packages',
'E:\\python\\PYTHON and GIT\\lib\\site-packages',
'E:\\python\\PYTHON and GIT\\lib\\site-packages\\pip-21.0.1-py3.9.egg',
'E:\\python\\PYTHON and GIT\\lib\\site-packages\\win32',
'E:\\python\\PYTHON and GIT\\lib\\site-packages\\win32\\lib',
'E:\\python\\PYTHON and GIT\\lib\\site-packages\\Pythonwin']
Server time: Sun, 03 Oct 2021 15:47:10 +0000
I have included the following code to support the argument.我包含了以下代码来支持该论点。
MODELS.PY模型.PY
from django.db import models
from django.db.models.fields import CharField
from django.utils import timezone
# Create your models here.
class ConferenceModel(models.Model):
conference_title = models.TextField(null=False,primary_key=True)
conference_description = models.CharField(max_length=1000,null=False)
conference_start_date = models.DateField(null=False)
conference_end_date = models.DateField(null=False)
class TalkModel(models.Model):
talk_conference_title = models.ForeignKey(ConferenceModel,on_delete=models.CASCADE)
talk_title = models.TextField(null=False,primary_key=True)
talk_description = models.CharField(max_length=100,null=False)
talk_duration = models.DecimalField(max_digits=2,decimal_places=1)
talk_time = models.DateTimeField(null=False)
class SpeakerModal(models.Model):
speaker_talk_title = models.ForeignKey(TalkModel,on_delete=models.CASCADE)
speaker_username = models.CharField(max_length=25,null=False)
speaker_email = models.EmailField(max_length=100,primary_key=True,null=False)
class ParticipantModel(models.Model):
participant_talk_title = models.ForeignKey(TalkModel,on_delete=models.CASCADE)
participant_username = models.CharField(max_length=25,null=False)
participant_email = models.EmailField(max_length=100,primary_key=True,null=False)
URLS.PY网址.py
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('',views.conferenceView,name='conference'),
path('delete/<str:id>',views.conferencedelete,name='conferencedelete'),
path('<str:id>/edit',views.conferenceedit,name='conferenceedit'),
path('<str:id>/talks/',views.talkView,name='talks'),
path('admin/', admin.site.urls),
]
VIEWS.PY视图.PY
from django.shortcuts import render
from django import forms
from django.shortcuts import render,HttpResponsePermanentRedirect,HttpResponse
from myapp.forms import ConferenceForm,TalkForm,SpeakerForm,ParticipantForm
from myapp.models import ConferenceModel,TalkModel,SpeakerModal,ParticipantModel
# Create your views here.
def conferenceView(request):
if request.method == 'POST':
conferenceform = ConferenceForm(request.POST)
if conferenceform.is_valid():
conferenceform.save()
conferenceform = ConferenceForm()
else:
conferenceform = ConferenceForm()
conference = ConferenceModel.objects.all()
return render(request,'myapp/conference.html',{'conference':conference,'conferenceform':conferenceform})
def conferenceedit(request,id):
if request.method == 'POST':
uniqueconferencetitle = ConferenceModel.objects.get(pk=id)
requestconferencedetails = ConferenceForm(request.POST,instance=uniqueconferencetitle)
if requestconferencedetails.is_valid():
requestconferencedetails.save()
return HttpResponsePermanentRedirect('/')
else:
uniqueconferencetitle = ConferenceModel.objects.get(pk=id)
requestconferencedetails = ConferenceForm(instance=uniqueconferencetitle)
return render(request,'myapp/conferenceedit.html',{'requestconferencedetails':requestconferencedetails})
def conferencedelete(request,id):
if request.method == 'POST':
conferencedelete = ConferenceModel.objects.get(pk=id)
conferencedelete.delete()
return HttpResponsePermanentRedirect('/')
def talkView(request,id):
talk = ConferenceModel.objects.get(pk=id)
conferencetalks = talk.talkmodel_set.all()
return render(request,'myapp/talks.html',{'conferencetalks':conferencetalks})
FORMS.PY FORMS.PY
from django import forms
from django.db import models
from django.db.models import fields
from django.forms import widgets
from myapp.models import ConferenceModel,TalkModel,SpeakerModal,ParticipantModel
class ConferenceForm(forms.ModelForm):
class Meta:
model = ConferenceModel
fields = ['conference_title','conference_description','conference_start_date','conference_end_date']
widgets = {
'conference_title' : forms.TextInput(attrs={'class':'form-control '}),
'conference_description' : forms.TextInput(attrs={'class':'form-control'}),
'conference_start_date' : forms.TextInput(attrs={'class':'form-control','placeholder':'YYYY-MM-DD'}),
'conference_end_date' : forms.TextInput(attrs={'class':'form-control','placeholder':'YYYY-MM-DD'})
}
class TalkForm(forms.ModelForm):
class Meta:
model = TalkModel
fields = ['talk_conference_title','talk_title','talk_description','talk_duration','talk_time']
widgets = {
'talk_conference_title' : forms.HiddenInput,
'talk_title' : forms.TextInput(attrs={'class':'form-control '}),
'talk_description' : forms.TextInput(attrs={'class':'form-control '}),
'talk_duration' : forms.TextInput(attrs={'class':'form-control '}),
'talk_time' : forms.TextInput(attrs={'class':'form-control '}),
}
class SpeakerForm(forms.ModelForm):
class Meta:
model = SpeakerModal
fields = ['speaker_talk_title','speaker_username','speaker_email']
widgets = {
'speaker_talk_title' : forms.HiddenInput,
'speaker_username' : forms.TextInput(attrs={'class':'form-control '}),
'speaker_email' : forms.TextInput(attrs={'class':'form-control '})
}
class ParticipantForm(forms.ModelForm):
class Meta:
model = ParticipantModel
fields = ['participant_talk_title' , 'participant_username' , 'participant_email']
widgets = {
'participant_talk_title' : forms.HiddenInput,
'participant_username' : forms.TextInput(attrs={'class':'form-control '}),
'participant_email' : forms.TextInput(attrs={'class':'form-control '})
}
CONFERENCE.HTML会议.HTML
{% extends 'myapp/base.html' %}
{% block content %}
<div class="col-sm-5 container">
<h4 class="alert alert-info">Add a new conference</h4>
<form class="form-control" method="POST">
{% csrf_token %}
{{ conferenceform }}
<p></p>
<input type="submit" class="btn btn-success " value="ADD" id="add">
</form>
</div>
<div class="container mt-3">
<h4 class="alert alert-info text-center">
Here is the list of confrences
</h4>
<table class="table table-hover">
<th class="thead-dark">
<tr>
<th scope="col">Title</th>
<th scope="col">Description</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Options</th>
</tr>
</th>
{% for conferences in conference %}
<tr>
<th scope="col"><a href="{% url 'talks' conferences.conference_title %}">{{conferences.conference_title}}</a></th>
<th scope="col">{{conferences.conference_description}}</th>
<th scope="col">{{conferences.conference_start_date}}</th>
<th scope="col">{{conferences.conference_end_date}}</th>
<td>
<a href="{% url 'conferenceedit' conferences.conference_title %}" class="btn btn-danger btn-sm">Edit</a>
<form action="{% url 'conferencedelete' conferences.conference_title %}" method="POST" class="d-inline">
{% csrf_token %}
<input type="submit" value="Delete" class="btn btn-danger btn-sm">
</form>
</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock content %}
TALKS.HTML谈话.HTML
{% extends 'myapp/base.html' %}
{% block content %}
<div class="container mt-3">
<h4 class="alert alert-info text-center">
Here is the list of Talks under this conference
</h4>
<table class="table table-hover">
<th class="thead-dark">
<tr>
<th scope="col">Talk Title</th>
<th scope="col">Talk Description</th>
<th scope="col">Talk Duration </th>
<th scope="col">Talk Time</th>
<th scope="col">Options</th>
</tr>
</th>
{% for talkss in conferencetalks %}
<tr>
<th scope="col">{{talkss.talk_title}}</th>
<th scope="col">{{talkss.talk_description}}</th>
<th scope="col">{{talkss.talk_duration}}</th>
<th scope="col">{{talkss.talk_time}}</th>
<!-- <td>
<a href="{% url 'conferenceedit' conferences.conference_title %}" class="btn btn-danger btn-sm">Edit</a>
<form action="{% url 'conferencedelete' conferences.conference_title %}" method="POST" class="d-inline">
{% csrf_token %}
<input type="submit" value="Delete" class="btn btn-danger btn-sm">
</form>
</td> -->
</tr>
{% endfor %}
</table>
</div>
{% endblock content %}
You loop through your queryset
by naming the item conferences
.您通过命名项目
queryset
来遍历您的conferences
集。
So to access the Id, since the URL expects an id.因此要访问 Id,因为 URL 需要一个 id。 Look this
path('<str:id>/edit',views.conferenceedit,name='conferenceedit'),
.看这个
path('<str:id>/edit',views.conferenceedit,name='conferenceedit'),
。
You need to do this:你需要这样做:
{% url 'conferenceedit' conferences.id %}
Or或者
{% url 'conferenceedit' conferences.pk %}
PS.附言。 Improve your path by updating
'<str:id>
to '<int:id>
, since your model is integer.通过将
'<str:id>
更新为'<int:id>
> 来改进您的路径,因为您的 model 是 integer。
urls.py网址.py
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('',views.conferenceView,name='conference'),
path('delete/<int:id>',views.conferencedelete,name='conferencedelete'),
path('<int:id>/edit',views.conferenceedit,name='conferenceedit'),
path('<int:id>/talks/',views.talkView,name='talks'),
path('admin/', admin.site.urls),
]
talks.html谈话.html
{% extends 'myapp/base.html' %}
{% block content %}
<div class="container mt-3">
<h4 class="alert alert-info text-center">
Here is the list of Talks under this conference
</h4>
<table class="table table-hover">
<th class="thead-dark">
<tr>
<th scope="col">Talk Title</th>
<th scope="col">Talk Description</th>
<th scope="col">Talk Duration </th>
<th scope="col">Talk Time</th>
<th scope="col">Options</th>
</tr>
</th>
{% for talkss in conferencetalks %}
<tr>
<th scope="col">{{talkss.talk_title}}</th>
<th scope="col">{{talkss.talk_description}}</th>
<th scope="col">{{talkss.talk_duration}}</th>
<th scope="col">{{talkss.talk_time}}</th>
<!-- <td>
<a href="{% url 'conferenceedit' talkss.talk_conference_title_id %}" class="btn btn-danger btn-sm">Edit</a>
<form action="{% url 'conferencedelete' talkss.talk_conference_title_id %}" method="POST" class="d-inline">
{% csrf_token %}
<input type="submit" value="Delete" class="btn btn-danger btn-sm">
</form>
</td> -->
</tr>
{% endfor %}
</table>
</div>
{% endblock content %}
conference.html会议.html
{% extends 'myapp/base.html' %}
{% block content %}
<div class="col-sm-5 container">
<h4 class="alert alert-info">Add a new conference</h4>
<form class="form-control" method="POST">
{% csrf_token %}
{{ conferenceform }}
<p></p>
<input type="submit" class="btn btn-success " value="ADD" id="add">
</form>
</div>
<div class="container mt-3">
<h4 class="alert alert-info text-center">
Here is the list of confrences
</h4>
<table class="table table-hover">
<th class="thead-dark">
<tr>
<th scope="col">Title</th>
<th scope="col">Description</th>
<th scope="col">Start Date</th>
<th scope="col">End Date</th>
<th scope="col">Options</th>
</tr>
</th>
{% for conferences in conference %}
<tr>
<th scope="col"><a href="{% url 'talks' conferences.pk %}">{{conferences.conference_title}}</a></th>
<th scope="col">{{conferences.conference_description}}</th>
<th scope="col">{{conferences.conference_start_date}}</th>
<th scope="col">{{conferences.conference_end_date}}</th>
<td>
<a href="{% url 'conferenceedit' conferences.pk %}" class="btn btn-danger btn-sm">Edit</a>
<form action="{% url 'conferencedelete' conferences.pk %}" method="POST" class="d-inline">
{% csrf_token %}
<input type="submit" value="Delete" class="btn btn-danger btn-sm">
</form>
</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock content %}
I got the problem was with my TALKS.HTML, I duplicated this HTML from the CONFERENCE.HTML and missed removing the following code.我发现问题出在我的 TALKS.HTML 上,我从 CONFERENCE.HTML 中复制了这个 HTML,但没有删除以下代码。
<!-- <td>
<a href="{% url 'conferenceedit' conferences.conference_title %}" class="btn btn-danger btn-sm">Edit</a>
<form action="{% url 'conferencedelete' conferences.conference_title %}" method="POST" class="d-inline">
{% csrf_token %}
<input type="submit" value="Delete" class="btn btn-danger btn-sm">
</form>
</td> -->
though I commented it, but it seems it wasn't commented, I removed it and now it works虽然我评论了它,但它似乎没有评论,我删除了它,现在可以使用了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.