Django:在第二个模板中添加可点击链接的问题 [英] Django: Problem with Adding Clickable Link in the Second Template
问题描述
我是Django的初学者.我正在构建一个名为PhoneReview的Django应用.它将存储与最新手机有关的评论.它还将显示电话品牌以及相关的电话型号.
I am a beginner in Django. I am building a Django app, named PhoneReview. It will store reviews related to the latest mobile phone. It will also display phone brands, along with the associated phone models.
我已经创建了模型和视图.我还设法在第一个模板(brandlist.html)中添加了可点击的链接.在第一个模板中,当您单击品牌名称(例如Samsung)时,您将被带到手机型号的页面(例如Galaxy S10).这是第一个模板的屏幕截图:
I have already created models and views. I have also managed to add clickable link in the first template (brandlist.html). In the first template, when you click on the brand name, like Samsung, you will be taken to the page of the phone model, like Galaxy S10. Here is the screenshot of the first template:
单击链接时,将带您到第二个模板(phonemodel.html).但是现在,我面临一个问题.手机型号("Galaxy S10")上没有可点击的链接,该链接可将您引向details.html.这是屏幕截图.
When you click the link, you will be taken to the second template (phonemodel.html). But now, I am facing an issue. There is no clickable link on the phone model ("Galaxy S10") that will direct you to details.html. Here is the screenshot.
以下是"PhoneReview"文件夹中models.py的代码:
Here are the codes of models.py inside the "PhoneReview" folder:
from django.db import models
from django.template.defaultfilters import slugify
# Create your models here.
class Brand(models.Model):
brand_name = models.CharField(max_length=100)
origin = models.CharField(max_length=100)
manufacturing_since = models.CharField(max_length=100, null=True, blank=True)
def __str__(self):
return self.brand_name
def save(self, *args, **kwargs):
self.slug = slugify(self.brand_name)
super().save(*args, **kwargs)
class PhoneModel(models.Model):
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
model_name = models.CharField(max_length=100)
launch_date = models.CharField(max_length=100)
platform = models.CharField(max_length=100)
def __str__(self):
return self.model_name
class Review(models.Model):
phone_model = models.ManyToManyField(PhoneModel, related_name='reviews')
review_article = models.TextField()
date_published = models.DateField(auto_now=True)
slug = models.SlugField(max_length=150, null=True, blank=True)
def __str__(self):
return self.review_article
以下是"PhoneReview"文件夹中urls.py的代码:
Here are the codes of urls.py inside the "PhoneReview" folder:
from . import views
from django.urls import path
urlpatterns = [
path('index', views.BrandListView.as_view(), name='brandlist'),
path('phonemodel/<int:pk>/', views.ModelView.as_view(), name='modellist'),
path('details/<int:pk>/', views.ReviewView.as_view(), name='details'),
]
以下是"PhoneReview"文件夹中views.py的代码:
Here are the codes of views.py inside the "PhoneReview" folder:
from django.views import generic
from .models import Brand, PhoneModel, Review
class BrandListView(generic.ListView):
template_name = 'PhoneReview/brandlist.html'
context_object_name = 'all_brands'
def get_queryset(self):
return Brand.objects.all()
class ModelView(generic.DetailView):
model = PhoneModel
template_name = 'PhoneReview/phonemodel.html'
class ReviewView(generic.DetailView):
model = Review
template_name = 'PhoneReview/details.html'
以下是"PhoneReview"文件夹中的apps.py代码:
Here are the codes of apps.py inside the "PhoneReview" folder:
from django.apps import AppConfig
class PhonereviewConfig(AppConfig):
name = 'PhoneReview'
以下是"templates"文件夹中的details.html代码:
Here are the codes of details.html inside the "templates" folder:
{% extends 'PhoneReview/base.html' %}
{% load static %}
<html>
<link rel="stylesheet" type="text/css" href="{% static "css/style.css" %}">
<html lang="en">
{% block title%}Details{% endblock %}
{% block content %}
<h1>This is the Details Page</h1>
<h2>Review:</h2>
<p>{{ review.review_article }}</p>
<h2>News Link:</h2>
<p>{{ review.slug }}</p>
{% endblock %}
</html>
以下是模板"文件夹中phonemodel.html的代码:
Here are the codes of phonemodel.html inside the "templates" folder:
{% extends 'PhoneReview/base.html' %}
{% load static %}
{% block title%}
Phone Model Page
{% endblock %}
{% block content %}
<!--Page content-->
<h1>This is Phone Model Page</h1>
<h2>Here is the phone model</h2>
<ul>
<li>{{ phonemodel.model_name }}</li>
</ul>
<img src="{% static "images/brandlist.jpg" %}" alt="Super Mario Odyssey" /> <!-- New line -->
{% endblock %}
我尝试将< li> {{phonemodel.model_name}}</li>
替换为< li>< a href ="{%url'details'brand.id%}> {{phonemodel.model_name}}</a></li>
.但是我收到一个错误,看起来像这样:
I tried replacing <li>{{ phonemodel.model_name }}</li>
with <li><a href = "{% url 'details' brand.id %}">{{ phonemodel.model_name }}</a></li>
. But I get an error, which looks like this:
NoReverseMatch at /phonemodel/1/
Reverse for 'details' with arguments '('',)' not found. 1 pattern(s) tried: ['details/(?P<pk>[0-9]+)/$']
如何解决此问题?
推荐答案
没有名为 brand
的上下文变量,而且您还是不需要它.您应该使用 phonemodel
的 id
:
There is no context variable named brand
and you don't need it anyway. You should use the id
of the phonemodel
:
<li>
<a href = "{% url 'details' phonemodel.id %}">
{{ phonemodel.model_name }}
</a>
</li>
这篇关于Django:在第二个模板中添加可点击链接的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!