Hi, I am really junior into Django. Really loving it, but stuck on this for
2 weeks.
I have a models (ImpCalendar), which has a ForeignKey to Establishments. I
do not use that foreignkey in the view/form so i don't know if it is
relevant.
I want to have a form for the ImpCalendar so I can edit it.
The url should have the calendar_id, which i set in the urls.py
If i in views.py do;
return render(request, 'importer/calendaredit.html', {'form': calendar})
i do see the data on a page, but not a (edit) form. Just the data, not the
fill-in fields.
When i do
return render(request, 'importer/calendaredit.html', {'form': form})
which sounds logical to me, I get the error
django.urls.exceptions.NoReverseMatch: Reverse for 'calendaredit' with
arguments '('',)' not found. 1 pattern(s) tried: [
'importer\\/calendar\\/(?P<calendar_id>[0-9]+)\\/$']
It seems to me, the value returned for the calendar_id is now forms-data
(html) which it can not do anything with. But i don't know what I am doing
wrong.
models.py
class Impcalendar(models.Model):
establishment = models.ForeignKey(Establishments, on_delete=SET_NULL,
null=True)
url = models.CharField(max_length=255)
comment = models.CharField(max_length=255, null=True, blank=True)
status = models.IntegerField(null=True, blank=True)
def __str__(self):
return str(self.establishment)
forms.py
from django import forms
import datetime
from importer.models import Impcalendar, Establishments
class ImpcalendarForm(forms.ModelForm):
class Meta:
model = Impcalendar
fields = ['id', 'url']
urls.py
from django.urls import path
from importer import views
urlpatterns = [
path('', views.index, name='index'),
path('calendar/<int:calendar_id>/', views.calendaredit, name=
'calendaredit')
]
views.py
def calendaredit(request, calendar_id):
calendar = get_object_or_404(Impcalendar, pk=calendar_id)
print (calendar.url)
if request.method == 'POST':
form = ImpcalendarForm(request.POST, instance=calendar)
if form.is_valid():
calendar.save()
else:
form = ImpcalendarForm(request.POST or None, instance=calendar)
return render(request, 'importer/calendaredit.html', {'form': form})
calendaredit.html
{% extends 'importer/base.html' %}
{% block content %}
<h1>{{ form.id }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'calendaredit' form.id %}" method="post">
<div class="fieldWrapper">
{% csrf_token %}
{{ form.id }}
{{ form.url }}
</div>
<input type="submit" value="Save" />
</form>
{% endblock %}
I did read the tutorial and the forms documentation on the site, but i am
making a mistake a probably overlook.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/d40fd9e5-69fc-4c90-a6da-e685e57d6af2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.