Can't display the data entered in form in data

2019-08-27 22:16发布

问题:

The user enters the data in the form. But the data entered in the form doesn't get displayed in the Database.

views.py

def add(request):
    if request.method=='POST':
        form=FilesCreate(request.POST)
        if form.is_valid():
            form.save()
    return render(request,'plagiarism/page1.html',{'form':FilesCreate()})
def add2(request):
    if request.method=='POST':
        form2=FilesCreate2(request.POST)
        if form2.is_valid():
            form2.save()
    return render(request,'plagiarism/page2.html',{'form':FilesCreate2})

models.py

from django.db import models
class File1(models.Model):
    #user=models.ForeignKey(User)
    firstfile=models.CharField(max_length=1000, default="")
    #secondfile=models.CharField(max_length=1000)
    def __str__(self):
        return self.firstfile

plagiarism/page1.html

<h1>Enter your first file</h1>
<form action="file2/" method="post">
    {% csrf_token %}
    {% for field in form %}
    {{field}}
    <input type="submit" value="Submit file1"/>

    {% endfor %}
</form>

plagiarism/page2.html (displays page after clicking submit in page 1)

<h1>Enter your second file</h1>
<form action="plagiarism/file2/result/" method="post">
    {% csrf_token %}
    {% for field in form %}
    {{field}}
    <input type="submit" value="Get Results"/>

    {% endfor %}
    </form>
{% block h1 %}

    {% endblock %}
<body>

plagiarism/page3.html (displays page after clicking submit in page 2)

<h1> Here is your Result </h1>
    <h2>
        {{data}}
    </h2>
</body>

forms.py

from django.forms import ModelForm
from django import forms
from plagiarism.models import File1,File2
class FilesCreate(ModelForm):
    class Meta:
        model=File1
        exclude=()
        widgets={'firstfile':forms.Textarea(attrs={'cols':50,'rows':100})}

example.py

from django.shortcuts import render
def getresult(request):
    data=95.5
    return render(request,'plagiarism/page3.html',{'data': data})

urls.py

from django.conf.urls import url
from . import views
from . import example3
urlpatterns=[

    url(r'^$',views.add,name='add'),
    url(r'file2/$',views.add2,name='add2'),
url(r'file2/result/$',example3.getresult,name='getresult')
]

回答1:

You seem to want a kind of wizard, where you process a form and it redirects you to the next, but you're not doing the basics of form processing well. For simple form handling, you can do this:

urls.py

from django.conf.urls import url
from . import views
from . import example3

urlpatterns=[
    url(r'^$',views.add,name='add'),
    url(r'file2/result/$', example3.getresult, name='getresult')
]

In the template, you are calling file2 with the form's action, but you really want to call the same page, to process the form with the add view:

plagiarism/page1.html

<h1>Enter your first file</h1>
<form method="post">
    {% csrf_token %}
    {% for field in form %}
    {{field}}
    {% endfor %}
    <input type="submit" value="Submit file1"/>
</form>

Note the missing action attribute in the <form> element.

When you visit the root of the website, the add view will be called with a GET request. When you submit the form, the same add view will be called, with a POST request, which will then be processed:

views.py

def add(request):
    if request.method == 'POST':
        form = FilesCreate(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('getresult'))
    else:
        form = FilesCreate()
    return render(request,'plagiarism/page1.html',{'form': form})

Note the HttpResponseRedirect, which redirects to a new page on success, and the else, which creates an empty form for the first time you visit the page (i.e. request.method is not POST, it is GET). This way, if the form isn't valid, the last line will render it bound to the data that was submitted and display the errors.

This should get you the data into the database, which was your first goal. If you want to go to another form upon submission, you can redirect there (instead of the result page) and do the same as above in the view add2.

There used to be a Django Form Wizard, but you can see this project to do multi-step forms.