Ashish
Ashish

Reputation: 429

how to give an internal link to a submit button in django?

this is a part of my index.html page which generates a from :

<form id="category_form" method="post" action="/wiki/">

            {% csrf_token %}


            {% for field in form %}
                {{ field.errors }}
                {{ field.help_text }}
                {{ field }}
            {% endfor %}

           <input type="submit" name="submit" value="Create Category" />

when i click submit button it stores the inputted value my model.what i want to do now is provide a link to this submit button so that when i click it it must input the value entered to my model as well as go to the link

i tried doing this :

<a href='content.html'>  <input type="submit" name="submit" value="Create Category" /></a>

content.html is an another html view.the view.py which supply variables to content.html actually processes the values entered in that form above and returns some variable to content.html page below is my index view

def index(request):
    if request.method == 'POST':
        form = scrapform(request.POST)

        if form.is_valid():
            form.save(commit=True)
            return index(request)
        else:
            print ('from.errors')
    else:
        form = scrapform()

    return render(request, 'wiki/index.html', {'form': form})

below is forms.py

LEVEL_CHOICES = (('beg','beginner'),('ind','inter'),('exp','expert'))
class scrapform(forms.ModelForm):
    subject = forms.CharField(help_text="please enter the subject you desire")
    level = forms.CharField(max_length=128,widget=forms.Select(choices=LEVEL_CHOICES))
    time = forms.IntegerField(initial=0)

    class Meta:
        model = scrap
        fields = '__all__'

below is my content view

def content(request):
    subject = scrap.objects.values_list('subject', flat=True).distinct()
    level = scrap.objects.values_list('level', flat=True).distinct()
    time = scrap.objects.values_list('time', flat=True).distinct()
    page = wikipedia.page(subject)
    title = page.title
    content = page.content
    summary = page.summary
    html = page.html
    wiki_dict = {'summary':summary,'content':content,'title':title,'html':html}

    return render(request, 'wiki/content.html',wiki_dict)

Upvotes: 0

Views: 2335

Answers (1)

Shang Wang
Shang Wang

Reputation: 25539

Why don't you use redirect in your views.py? Unless the link is dynamic, you should be able to easily achieve this in your views:

from django.shortcuts import redirect
from django.core.urlresolvers import reverse

def index(request):
    if request.method == 'POST':
        form = scrapform(request.POST)

        if form.is_valid():
            # you don't actually need commit=True, that's the default behavior
            form.save()
            return redirect(reverse('url-for-content.html'))

Django doc for redirect and reverse.

Upvotes: 1

Related Questions