无法显示在数据库中以表格形式输入的数据 [英] Can't display the data entered in form in database

查看:54
本文介绍了无法显示在数据库中以表格形式输入的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户以表格形式输入数据.但是,在表单中输入的数据不会显示在数据库中.

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

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

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(在第1页中单击提交"后显示页面)

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(在第2页中单击提交"后显示页面)

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

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')
]

推荐答案

您似乎想要一种向导,您可以在其中处理表单并将其重定向到下一个向导,但您没有做表单处理的基础知识出色地.对于简单的表单处理,您可以执行以下操作:

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')
]

在模板中,您要使用表单的 action 调用 file2 ,但您确实要调用同一页面,以使用 add处理表单视图:

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:

抄袭/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>

请注意< form> 元素中缺少的 action 属性.

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

当您访问网站的根目录时,将通过 GET 请求调用 add 视图.提交表单时,将调用相同的 add 视图,并带有 POST 请求,然后将对其进行处理:

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

请注意 HttpResponseRedirect (成功后将重定向到新页面)和 else (这是您首次访问该页面时创建一个空白表格)的原因.code> request.method 不是 POST ,而是 GET ).这样,如果表单无效,则最后一行将使其绑定到提交的数据并显示错误.

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.

应该将数据带入数据库,这是您的第一个目标.如果要在提交时转至另一种表单,则可以重定向到该表单(而不是结果页面),并在视图 add2 中进行与上述相同的操作.

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.

曾经有一个Django表单向导,但是您可以看到此项目做多步骤表格.

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

这篇关于无法显示在数据库中以表格形式输入的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆