我在项目存储库“论坛”中有这样的urls.py
配置
# Project url
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
url(r'^article/', include('article.urls',namespace='article')),
]
,其中article/urls.py
为
#Article app's url config
urlpatterns = [
# show the article list
url(r"^list/(?P<block_id>\d+)$", views.article_list, name="article_list"),
我尝试将“ ^ article / list / 1 $”作为首页,而不是“ views.index”。
当我发出请求“ 127.0.0.1:8000”时,如何使其重定向到"^article/list/1$"
?
答案 0 :(得分:2)
您可以从您的views.index重定向。在您的views.py
from django.shortcuts import redirect
def index(request):
return redirect('article:article_list')
应该带您进入文章/列表。您可以在重定向功能中包含该block_id参数。
答案 1 :(得分:1)
尝试
redirect(reverse('article:article_list', kwargs={'block_id':2}))
并确保在这样的函数中添加kwargs
article_list(request,**kwargs):
答案 2 :(得分:0)
我假设您要以此调试您的文章页面。 您有两种方法可以做到这一点。
views.py
from django.shortcuts import redirect
def index(request):
#We force a redirect to article_list view, and we pass arguments.
#Notice it has to come first.
redirect ("article_list", article_list_argument_view=1)
#it will redirect to localhost:8000/article/list/1
'''
Your Home page code.
'''
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^$", views.index, name="index"),
#The order is important.
url(r'^article/list/1$', "article_list", {'article_list_argument_view':1})
url(r'^article/', include('article.urls',namespace='article')),
]