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