Reputation: 129
I am trying to create "Edit" for my form.
urls.py
url(r'^app_1/(?P<id>[-\w]+)/edit/$',views.edit, name = 'edit'),
forms.py
class ClanakForma(forms.ModelForm):
class Meta:
model = Clanak
fields = '__all__'
models.py
class Clanak(models.Model):
naslov = models.CharField(null=False, blank=True, max_length=120)
datumObjave = models.DateField(null=False, blank=False)
autor = models.CharField(null=False, blank=True, max_length=50)
email = models.EmailField(max_length=75, null=True, blank=True)
def __str__(self):
return str(self.naslov) + ', ' + str(self.datumObjave) + ', ' + str(self.autor)
views.py
def edit(request, id):
data = get_object_or_404(Clanak, id = id)
if request.method == "POST":
form = ClanakForma(request.Clanak, instance=data)
if form.is_vaild():
data = form.save(commit=False)
data.naslov = request.user
data.datumObjave = request.user
data.autor = request.user
data.email = request.user
return redirect('readAllNew')
else:
form = ClanakForma(instance=data)
template = 'readAllNew.html'
context = {'form': form}
return render(request, template, context)
readAllNew.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table border="1">
<tr>
<th>Naslov</th>
<th>Datum</th>
<th>Autor</th>
<th>Mail</th>
</tr>
{% for x in data %}
<tr>
<td>{{x.naslov}}</td>
<td>{{x.datumObjave}}</td>
<td>{{x.autor}}</td>
<td>{{x.email}}</td>
<td><a href="{% url 'delete' x.id %}">delete</a></td>
<td><a href="{% url 'edit' x.id %}">edit</a></td>
</tr>
{% endfor %}
</table>
</body>
</html>
So when I hit edit link at my "readAllNew.html" I get error:
TypeError at /app_1/5/edit/
init() got an unexpected keyword argument 'instance'
I think something is wrong with my view but I am not sure what.
-------------------------------UPDATE--------------------------------
As suggested I edited "forms.py" but now I am not seeing any form field so I can't edit nothing:
---------------- UPDATE 2 -------------------------
I created new html file as "edit.html"
I edited "views.py":
def edit(request, id):
data = get_object_or_404(Clanak, id = id)
if request.method == "POST":
form = ClanakForma(instance=data)
if form.is_vaild():
data = form.save(commit=False)
data.naslov = request.user
data.datumObjave = request.user
data.autor = request.user
data.email = request.user
return redirect('readAllNew.html')
else:
form = ClanakForma(instance=data)
template = 'edit.html'
context = {'form': form}
return render(request, template, context)
Now I see fields and informations in it properly, but when I change something and click on "SUBMIT" I get error:
AttributeError at /app_1/6/edit/
'ClanakForma' object has no attribute 'is_vaild'
Upvotes: 1
Views: 1087
Reputation: 88539
Use forms.ModelForm
instead of forms.Form
class Forma(forms.ModelForm):
naslov = forms.CharField(label='naslov')
datumObjave = forms.DateField(label='datumObjave')
autor = forms.CharField(label='autor')
email = forms.EmailField(label='email')
class Meta:
model = Clanak
fields = ('naslov', 'datumObjave', 'autor', 'email')
If you want to use all the fields of Model class in your form, you could specify the fields
attribute of Meta
class as fields = '__all__'
#example
class Forma(forms.ModelForm):
class Meta:
model = Clanak
fields = '__all__'
If you are using forms.ModelForm
, you could avoid most of the implementation which makes things easier :)
You need to render the form in your template.So, change your template as
<form method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
Upvotes: 1
Reputation: 3399
You can't pass instance
to simple form - it has to be ModelForm
. Use this:
class Forma(forms.ModelForm):
class Meta:
model = Clanak
fields = ['naslov', 'datumObjave', 'autor', 'email']
labels = {
'naslov': 'naslov',
'datumObjave': 'datumObjave',
'autor': 'autor',
'email': 'email'
}
Upvotes: 0