无法在Django中处理简单的表单?

时间:2012-11-06 09:58:08

标签: python django forms

我遇到一个简单的表单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变量中,稍后我计划将其存储在数据库中。

2 个答案:

答案 0 :(得分: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))

根据情况,您的视图只会将数据存储到某些变量中,然后再发出重定向。

修改 - 另外,请查看有关上传文件的文档:link。下次加入UploadFileForm代码也是一个好主意。

答案 1 :(得分:1)

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

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

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

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