为什么我的代码不能像“http://127.0.0.1:8000/movie/xyz”那样转到动态url?xyz
是电影名。Demo是我模特的名字。
这是我的观点.py:
from django.shortcuts import render
from .models import Demo
def home(request):
movie_list = Demo.objects.all()
return render(request, 'home.html', {movie_list': movie_list,})
def movie_detail(request,title):
detail = Demo.objects.get(title=title)
return render(request, 'detail.html', {'detail': detail})
网址.py:
from django.conf.urls import include, url
from django.contrib import admin
from netflix.views import home,movie_detail
urlpatterns = [url(r'^admin/', include(admin.site.urls)),
url(r'^movie', home),
url(r'^movie/(?P<title>.*)/$', movie_detail, name='movie_detail')]
detail.html:详细信息:
<div class="container-fluid">
<div class="row">
<div class="card mb-3">
<img class="card-img-top" src="{{detail.img_url}}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">{{detail.title}}</h5>
</div>
</div>
最佳答案
您需要在/movie URL中添加一个终止$
,否则它将匹配以该字符串开头的所有内容。
url(r'^movie$', home),
关于python - 不要重定向到动态网址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49380282/