edit page (form) not shown

13 views
Skip to first unread message

BeginnerB

unread,
Jul 7, 2018, 8:06:45 AM7/7/18
to Django users
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.
Reply all
Reply to author
Forward
0 new messages