查看表示表单的函数,从不出现在模板中。 这是我的课程
from django import forms
class SearchForm(forms.Form):
search_compNum = forms.CharField(label='Compound_Number', max_length=10)
search_formula = forms.CharField(label='Molecule_Formula')
search_format = forms.CharField(label='InChI or Smiles', widget=forms.Textarea)
这是视图功能。
from django.shortcuts import render
from .forms import SearchForm
def get_query(request):
""" molecule search form. """
form_class = SearchForm
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
Query_Compound_Number = request.POST.get('search_compNum', '')
else:
form_class = SearchForm()
return render(request, 'search.html', {'form': form_class})
这是一个模板。
{% extends "index.html" %}
{% block content %}
<form action="{% url 'chemdb:results' %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Search"/>
</form>
{% endblock %}
和urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^index/$', views.index, name='index'),
url(r'^search/$', views.SettingView.as_view(), name='search'),
url(r'^search/$', views.DataBaseView.as_view(), name='search'),
url(r'^search/$', views.SortView.as_view(), name='search'),
url(r'^search/$', views.TypeView.as_view(), name='search'),
url(r'^search/$', views.FilterView.as_view(), name='search'),
url(r'^search/$', views.get_query),
url(r'^results/$', views.result, name='results')
]
我正在使用Django 1.8.x版本。所以,我不需要导入'模式'。 帮我PLZ !!!!