0

我在使用简单的 POST 表单时遇到问题。这是我的 url.py:

urlpatterns = patterns('',

    url(r'^admin/', include(admin.site.urls)),
    url(r'^', 'fileupload.views.show_template'), 
    url(r'^upload_file/', 'fileupload.views.upload_file'),
)

这是我的form.html:

form action="/upload_file/" method="post">
    {% csrf_token %}
        <fieldset>
            <legend>Session</legend>
                <label for="input-one" class="float"><strong>Session Name:</strong></label><br />
                <input class="inp-text" name="input-one-name" id="session" type="text" size="30" /><br />

                <label for="input-two" class="float"><strong>Source:</strong></label><br />
                <input class="inp-text" name="input-two-name"  id="source" type="text" size="30" />

                <label for="input-two" class="float"><strong>Destination:</strong></label><br />
                <input class="inp-text" name="input-two-name"  id="destination" type="text" size="30" />
        </fieldset>



        <p><input class="submit-button" type="submit" alt="SUBMIT" name="Submit" value="Save" /></p>
        <p><input class="cancel-button" type="reset"  name="Save" value="Cancel" /></p>
    </form>

这是我的views.py:

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from fileupload.forms import UploadFileForm
from django.template import RequestContext


def show_template(request):
    return render_to_response('form.html', {'form': 'form'},  context_instance=RequestContext(request))

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST)
        if form.is_valid():
            sessiong = form.cleaned_data['session']
            sourceg = form.cleaned_data['source']
            destinationg = form.cleaned_data['destination']
            return HttpResponseRedirect('/thanks/') 
    else:
        form = UploadFileForm()
    return render_to_response('form.html', {'form': form},  context_instance=RequestContext(request))

问题是每当我填写表格并点击保存表格时根本没有处理,这显然意味着我的视图 upload_file 根本不起作用。可能是什么问题?简单的逻辑是:用户填写表格,单击保存,数据存储在 python 变量中,稍后我计划将其存储在数据库中。

4

2 回答 2

1

它不是在处理,因为你没有告诉它做任何事情。改变你的看法,看起来像

def upload_file(request):
if request.method == 'POST':
    form = UploadFileForm(request.POST)
    if form.is_valid():
        sessiong = form.cleaned_data['session']
        sourceg = form.cleaned_data['source']
        destinationg = form.cleaned_data['destination']
        o = Object(foo=sessiong, bar=sourceg) # This, or whatever you want to *do* with this data
        o.save()
        return HttpResponseRedirect('/thanks/') 
else:
    form = UploadFileForm()
return render_to_response('form.html', {'form': form},  context_instance=RequestContext(request))

就目前情况而言,您的视图只是将数据存储到一些变量中,然后发出重定向。

编辑- 另外,请查看有关上传文件的文档:链接。下次包含您的 UploadFileForm 代码也是一个好主意。

于 2012-11-06T10:08:06.153 回答
1

如果要将表单数据存储在数据库中,则需要在案例示例中为其创建模型

模型.py:

 from django.db import models
 class Upload(models.Model):
 session = models.CharField(max_length=200)
 Source = models.CharField(max_length=200)
 Destination = models.CharField(max_length=200)

视图.py:

 def def upload_file(request):
     if request.method == 'POST':
         session = request.POST.get('input-one-name')
         source = request.POST.get('input-two-name')
         destination = request.POST.get('input-three-name') #edit in your html page
         object = Upload() # this will create a object for your model
         object.Session = session
         object.Source = source
         object.Destination = destination
         object.save()
         return HttpResponseRedirect('/thanks/') 
    else:
        return render_to_response('form.html', {'form': form},context_instance=RequestContext(request))

这应该将表单信息保存在数据库中

于 2012-11-06T10:49:25.420 回答