这里有一个Django / Python新手。我使用的是python 3.4和Django 1.7版本。
我试图让我的索引页面加载超链接,我看到了这个问题。
我的index.html(删除不需要的行)
<form action="{% url 'testapp:search' %}" method="get">
{% csrf_token %}
<a href="{% url 'testapp:detail' morphological %}"">Morphological</a>
</form>
urls.py
from django.conf.urls import patterns, url
from testapp import views
urlpatterns = patterns('',
#url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^$', 'testapp.views.index', name='index'),
#url(r'^search-form/$', views.IndexView.as_view(), name='index'),
url(r'^search-form/$', 'testapp.views.index', name='index'),
#url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
#url(r'^option=(?P<option>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^detail/(?P<val>\w+)/?$', 'testapp.views.detail', name='detail'),
#url(r'^search/$', views.search, name='search'),
url(r'^search/$', views.search, name='search'),
)
views.py
from django.shortcuts import render, get_object_or_404
from django.shortcuts import render_to_response
# Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
#from polls.models import Question, Choice
from testapp.models import Molecular, Morphological
#from django.template import RequestContext, loader
from django.http import Http404
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.views import generic
def index(request):
return render_to_response('testapp/index.html', context_instance=RequestContext(request))
def search(request):
if 'q' in request.GET:
message = 'You searched for: %r' % request.GET['q']
else:
message = 'You submitted an empty form.'
return HttpResponse(message)
def detail(request, val):
message = 'hello form.'
return HttpResponse(message)
#class DetailView(generic.DetailView):
# model = Morphological
# template_name = 'testapp/detail.html'
如果单击链接并尝试使用该参数查询数据库,则尝试传递参数。我尝试了各种各样的东西,&amp;此时我被卡住了。我想要一个字符串,而不是我在标准民意调查应用程序中使用的主键。这只是我的正则表达式错了吗?
任何帮助解决这个问题都会有很大帮助。感谢。
答案 0 :(得分:5)
在网址中的参数周围放置单引号:
<a href="{% url 'testapp:detail' 'morphological' %}"">Morphological</a>