Django;正确显示项目的问题

时间:2019-12-04 06:27:05

标签: django

我正在通过构建一个名为TravelBuddies的应用程序来学习Django。它将允许旅行者计划行程并保留相关的旅行项目(例如预订,机票,护照副本,保险信息等),以及为日常活动创建提醒。该应用程序还将能够向旅行者更新本地信息,例如天气或每日新闻。旅行者还可以与某人共享旅行信息,或者让某人与他们合作来计划旅行。

我遇到了问题。我通过Django管理员为吉隆坡添加了两个活动。他们是“去植物园”和“去水族馆”。

当我转到http://127.0.0.1:8000/triplist/时,看到以下页面: enter image description here

当我点击吉隆坡时,我在http://127.0.0.1:8000/triplist/kuala-lumpur/上看到此页面: enter image description here

这两个活动分别称为“去植物园”和“去水瓶座”。它们应该以这种方式显示:

Activity name: Going to Botanical Garden
Date: Dec. 4, 2019
Time: 12:34 p.m.
Location: KL Sentral
Item Type: Ticket
Item Number: E12342


Activity name: Going to Aquaria
Date: Dec. 4, 2019
Time: 1:58 p.m.
Location: Bukit Bintang
Item Type: Ticket
Item Number: C45776

这意味着这两个活动之间必须存在差距。但是我做不到。

这是我在models.py中的代码:

from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from django.template.defaultfilters import slugify


# class Coplanner(models.Model):
#     coplanner_name = models.CharField(max_length=20)
#
#     def __str__(self):
#         return self.coplanner_name

class Trip(models.Model):
    trip_name = models.CharField(max_length=100)
    planner_name = models.CharField(max_length=100)
    add_coplanner = models.ManyToManyField(User)
    slug = models.SlugField(max_length=150, default='null')

    def __str__(self):
        return self.trip_name

    def save(self, *args, **kwargs):
        self.slug = slugify(self.trip_name)
        super().save(*args, **kwargs)

class Activity(models.Model):
    trip = models.ForeignKey(Trip, on_delete=models.CASCADE)
    activity_name = models.CharField(max_length=100)
    date = models.DateField(auto_now=True)
    time = models.TimeField(auto_now= True)
    location = models.CharField(max_length=100)
    item_type = models.CharField(max_length=100)
    item_number = models.CharField(max_length=100)
    slug = models.SlugField(max_length=150, default='null')

    def __str__(self):
        return self.activity_name

    def save(self):
        super(Activity, self).save()
        self.slug = '%i-%s' % (
            self.id, slugify(self.trip.trip_name)
        )
        super(Activity, self).save()

这是我在views.py中的代码:

from django.views import generic
from .models import Trip, Activity


class TripListView(generic.ListView):
    template_name = 'trips/triplist.html'
    context_object_name = 'all_trips'

    def get_queryset(self):
        return Trip.objects.all()


class ActivityView(generic.DetailView):
    model = Trip
    template_name = 'trips/activity.html'

这是我在urls.py中的代码:

from . import views
from django.urls import path

app_name = 'trips'

urlpatterns = [
    path('triplist/', views.TripListView.as_view(), name='triplist'),
    path('triplist/<slug:slug>/', views.ActivityView.as_view(), name='activity'),
]

这是我在apps.py中的代码:

from django.apps import AppConfig


class TripsConfig(AppConfig):
    name = 'trips'

这是我在triplist.html中的代码:

<!DOCTYPE html>
{% extends 'trips/base.html' %}
{% load static %}
<html lang="en">
<link rel="stylesheet" type="text/css" href="{% static "css/style.css" %}">

<head>
    <meta charset="UTF-8">
    {% block title%}Trip list{% endblock %}
    <title>Trip list</title>
</head>


<body>
    {% block content %}
    <!--Page content-->
    <h1>This is Trip List Page</h1>


    <ul>
        {% for trip in all_trips %}
            <li><a href="{% url 'trips:activity' trip.slug %}">{{ trip.trip_name }}</a></li>
        {% endfor %}


    </ul>

    <img src="{% static "images/botanical-garden.jpg" %}" alt="Botanical Garden" />
    <!-- New line -->
    {% endblock %}
</body>
</html>

</html>

这是我在activity.html中的代码:

{% extends 'trips/base.html' %}
{% block title%}
Detail
{% endblock %}


{% block content %}

<h3>These are the details for {{trip.trip_name}} </h3>

<ul>{% for trip_item in trip.activity_set.all %}
    <li>Activity name: {{ trip_item.activity_name }}</li>
    <li>Date: {{ trip_item.date }}</li>
    <li>Time: {{ trip_item.time }}</li>
    <li>Location: {{ trip_item.location }}</li>
    <li>Item Type: {{ trip_item.item_type }}</li>
    <li>Item Number: {{ trip_item.item_number }}</li>
    {% endfor %}
</ul>

{% endblock %}

views.pyactivity.html有什么问题吗?

1 个答案:

答案 0 :(得分:1)

可能您需要将<ul>标记放在for循环内:

{% for trip_item in trip.activity_set.all %}
<ul>
    <li>Activity name: {{ trip_item.activity_name }}</li>
    <li>Date: {{ trip_item.date }}</li>
    <li>Time: {{ trip_item.time }}</li>
    <li>Location: {{ trip_item.location }}</li>
    <li>Item Type: {{ trip_item.item_type }}</li>
    <li>Item Number: {{ trip_item.item_number }}</li>
</ul>
{% endfor %}