user3891554
user3891554

Reputation: 81

Passing data from template to view in Django

From a Django template, I pass an id as a parameter along with its url.

<a href="/add/?q={{mytable.id}}" >Next</a>

When I click next, it goes to the page 'add' with id parameter (localhost/add/?q=5c23451). I assign the value of this parameter to a variable.

q1=request.GET.get('q','')

Then, I try to get all the objects from the database where id='id' as follows:

obj=mytable.objects.get(id='q1')

The problem is when I try to print q1 it is always empty. Could you please tell me where am going wrong?

Entire View

def addview(request):
    q1=request.GET.get('q','')
    if request.method == 'POST':
        form= myform(request.POST)
        if form.is_valid():
            obj=mytable.objects.get(id=q1)
            obj.info = form.cleaned_data['info']
            obj.url= form.cleaned_data['content']
            obj.save()
        return HttpResponseRedirect('/success/')

    else:
        form=myform()
    variables=RequestContext(request, {
    'form':form
    })

    return render_to_response('/add/add.html', variables,)

forms.py

class myform(forms.Form):

info=forms.CharField(max_length=100)
content=forms.URLField()

Upvotes: 3

Views: 9853

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599470

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})

Upvotes: 6

itzMEonTV
itzMEonTV

Reputation: 20339

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

Upvotes: 0

Related Questions