6👍
You’re querying the string "q1"
rather than the variable q1
.
obj = mytable.objects.get(id=q1)
Note that lines in Python shouldn’t end with semicolons.
Edit
This is happening when you POST the form, not on the initial GET. That’s because GET variables aren’t preserved when you submit the form.
You really need to go back to basics on this. Passing IDs via GET parameters is not the right way to do things in Django. You should pass it in the URL itself; that is more easily produced by the built-in tools, and the value will be preserved when you submit.
Firstly, your addview
URL needs to accept the parameter:
url(r'^add/(?P<id>\d+)/$', views.addview, name='add'),
and your link should now use the {% url %}
template tag:
<a href="{% url "add" id=mytable.id %}" >Next</a>
Now, your view needs to accept that parameter:
def addview(request, id):
obj = mytable.objects.get(id=id)
This will fix your problem by itself, but there is another improvement you should make, which is to use a ModelForm rather than a plain form. Change the form as follows:
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('info', 'url')
and now your view becomes:
def addview(request, id):
obj = mytable.objects.get(id=id)
if request.method == 'POST':
form = MyForm(request.POST, instance=obj)
if form.is_valid():
form.save()
return redirect('success')
else:
form = MyForm(instance=obj)
return render(request, 'add/add.html', {'form': form})
0👍
The thing you are trying is incorrect. a href
is using for GET
request. If you click that link, it will not post any data.Also it will not enter into if request.method == 'POST'
condition.Then if you have a form to POST
.Then set form attribute action='/add/?q={{mytable.id}}'
.Then it will have request.POST
and request.GET
(now).
- [Django]-Django core: <django.utils.functional.__proxy__ object at ….> is not JSON serializable
- [Django]-Add E-Mail header in django
- [Django]-Django – how to restrict DeleteView to object owner