不要重定向到动态网址

时间:2018-03-20 09:18:09

标签: python django

为什么我无法使用我的代码转到" http://127.0.0.1:8000/movie/xyz " 这样的动态网址? xyz是电影名称。演示是我模特的名字。

这是我的view.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})

urls.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>

3 个答案:

答案 0 :(得分:3)

您需要在/ movie网址中添加终止$,否则它会将开头的所有内容与该字符串匹配。

url(r'^movie$', home),

答案 1 :(得分:1)

您错过url(r'^movie', home)

中的终止字符

试试这个:

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^movie$', home),
    url(r'^movie/(?P<title>.*)$', movie_detail, name='movie_detail')

]

答案 2 :(得分:0)

你可以这样做:

doOnUnsubscribe()