Django动态网址

时间:2017-09-04 17:30:55

标签: python html django

我尝试使用动态网址在海报内打开内容,但我遇到了问题 我的代码是包含一些电影海报的简单网页,当我点击海报时,新页面中的新模板应该打开,这将显示这张海报的信息。

但每当我点击海报时,相同的模板(index.html)都会在新页面中打开而不是page.html

例如

127.0.0.1:8000/home 是包含所有海报的网页,我点击了logo1,其中id = 1,然后在新页 127.0.0.1/home/1 将打开,但它仍然是index.html,所有海报都不是page.html,其中存储了poster1 id = 1的内容。

这是我的代码

主页/ models.py

from django.db import models

class Poster(models.Model):
    poster_name = models.CharField(max_length=20)
    poster_img = models.FileField(upload_to="poster_image/")

    def __str__(self):
        return self.poster_name


class Poster_page(models.Model):
    poster =  models.ForeignKey(Poster, default= 1)
    poster_name = models.CharField(max_length=20)
    poster_img = models.FileField()
    poster_details = models.TextField()

主页/ views.py

from django.shortcuts import render_to_response
from .models import Poster, Poster_page

def poster(request):
    pos = Poster.objects.all()
    return render_to_response('index.html', {'pos':pos})

def poster_page(request, id=1):
    poster_pg = Poster_page.objects.all()
    return render_to_response('page.html', {'poster_pg':poster_pg})

homepage.url

from django.conf.urls import url
from.views import poster, poster_page


urlpatterns = [
    url(r'^',poster),
    url(r'^home/(?P<id>/d+)/$', poster_page),

poster.url

from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^home/',include('homepage.urls', namespace='home'))
]


if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, 
    document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, 
    document_root=settings.MEDIA_ROOT)

的index.html

<body>
<div class="row">
    {% for poster in pos %}
    <div class="col-md-4">

            <a href="{{poster.id}}" target="new"><img src="{{ poster.poster_img.url }}" alt="image"><h3>{{ poster.poster_name }}</h3></a>
    </div>
    {% endfor %}
</div>

page.html中

<body>
<div class="row">
    <img src="{{ poster_img.url }}" alt="image"><h2>{{ poster_name }}</h2>
</div>
<div class="row">
    <h2>{{ poster_details }}</h2>       
</div>

3 个答案:

答案 0 :(得分:2)

homepage.url

  

d/+替换为\d+

from django.conf.urls import url
from . import views 

urlpatterns = [
    url(r'^$',views.poster),
    url(r'^(?P<id>\d+)/$', views.poster_page),
]

然后尝试:

from django.shortcuts import render,get_object_or_404

def poster_page(request, id):
    poster_pg = get_object_or_404(Poster_page, id=id)
    return render_to_response('page.html', {'poster_pg':poster_pg})

是的,您还需要 <a href="/home/{{poster.id}}/">

答案 1 :(得分:0)

在index.html中,您尚未在href中定义正确的网址以打开海报明细。

正确的语法是这样的:

<a href="home/{{poster.id}}" target="new"><img src="{{ poster.poster_img.url }}" alt="image"><h3>{{ poster.poster_name }}</h3></a>

答案 2 :(得分:0)

我认为问题出在上下文变量的视图中 -

return render_to_response('index.html', {'pos':pos})

应该是 -

return render_to_response('index.html', {'poster':pos})

因为您在索引模板中调用了{{poster.id}}