我正在尝试从html的div中的定位标记发送字符串值,即“ news.id” 到views.py。我不确定“ single_post / {{news.id}}”是否正确。 news.id可能是5c9f94516fe3c761e420333a(用于正则表达式)
.html
<div class="post-continue-btn">
<a href="single_post/{{ news.id }}" class="font-pt">Continue Reading
<i class="fa fa-chevron-right" aria-hidden="true"></i></a>
</div>
我已经以某种方式通过了这个,但是现在停留在URLs.py上,这是我的URL代码
URL.py
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^single_post/$', views.single_post),
url(r'^<slug:slug>/$',views.single_post),
url(r'^index/single_post/$', views.single_post),
url(r'^index/$', views.index),
url(r'^contact/$', views.contact),
url(r'^about_us/$', views.about_us),
path('admin/', admin.site.urls),]
views.py
def single_post(request):
project_name = request.GET.get('id')
context = {'doc_data': search_query.doc_data(project_name)}
return render_to_response('DeepNews/dummy.html', context)
如何在.html锚标记中正确编写代码,然后在URL.py中使用这种类型的字符串“ 5c9f94516fe3c761e420333a”的正则表达式是什么,这样我就可以在views.py中获取“ news.id”的值< / p>
答案 0 :(得分:1)
在您的网址上,将参数传递到单个帖子网址,并为其命名,以免在应用程序上使用硬编码的网址
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^single_post/(?P<uuid>[a-z0-9-]*)/', views.single_post, name='sigle-post'),
url(r'^index/$', views.index),
url(r'^contact/$', views.contact),
url(r'^about_us/$', views.about_us),
path('admin/', admin.site.urls),]
然后在模板上,您可以使用url
标签告诉django为您构造url
<div class="post-continue-btn">
<a href="{% url 'single-post' news.id %}" class="font-pt">Continue Reading
<i class="fa fa-chevron-right" aria-hidden="true"></i></a>
</div>
在您看来,您会收到论点
def single_post(request, uuid):
project_name = uuid
context = {'doc_data': search_query.doc_data(project_name)}
return render_to_response('DeepNews/dummy.html', context)
答案 1 :(得分:0)
在您的urls.py
中urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'(?P<single_post>[^/]+)/(?P<slug>[-\w]+)$', views.single_post, name='sigle-post'),
url(r'^index/single_post/$', views.single_post),
url(r'^index/$', views.index),
url(r'^contact/$', views.contact),
url(r'^about_us/$', views.about_us),
path('admin/', admin.site.urls),]