Home > front end >  Python Django Error during template rendering
Python Django Error during template rendering

Time:10-04

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.

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 Data/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

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

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

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

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

{% 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

{% 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 %}

CodePudding user response:

You loop through your queryset by naming the item conferences.

So to access the Id, since the URL expects an id. Look this 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.

CodePudding user response:

urls.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

{% 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

{% 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 %}

CodePudding user response:

I got the problem was with my TALKS.HTML , I duplicated this HTML from the CONFERENCE.HTML and missed removing the following code.

<!-- <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

  • Related