Django:分隔两个用户名的问题 [英] Django: Problem with Separating the Two User Names
问题描述
我正在通过构建一个名为TravelBuddies的应用程序来学习Django.它将允许旅行者计划行程并保留相关的旅行项目(例如预订,机票,护照副本,保险信息等),以及为日常活动创建提醒.该应用程序还将能够向旅行者更新本地信息,例如天气或每日新闻.旅行者还可以与某人共享旅行信息,或者让某人与他们合作以计划行程.
I am learning Django by building an application, called TravelBuddies. It will allow travelers to plan their trip and keep associated travel items (such as bookings, tickets, copy of passport, insurance information, etc), as well as create alerts for daily activities. The application will also able to update local information such as weather or daily news to the traveler. Travelers can also share the travel information with someone or have someone to collaborate with them to plan for the trip.
我遇到了问题.当我转到
I am facing a problem. When I go to http://127.0.0.1:8000/triplist/, I see this page:
当我点击旅行名称:吉隆坡并通过以下链接转到 activity.html
时:
When I click on Trip Name: Kuala Lumpur and taken to activity.html
through this link: http://127.0.0.1:8000/triplist/kuala-lumpur/:
"john"和"williams"是两个单独的用户名.它们需要用逗号(,)分隔.因此,它看起来像这样: Co-traveller:约翰,威廉姆斯
.我该怎么办?
"john" and "williams" are two separate user names. They need to be separated by a comma (,). So it will look like this: Co-traveller: john, williams
. How can I do it?
这是我在 models.py:
from django.contrib.auth.models import User
from django.db import models
from django.template.defaultfilters import slugify
# Create your models here.
class Trip(models.Model):
trip_name = models.CharField(max_length=100)
date = models.DateField()
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)
add_cotraveller = models.ManyToManyField(User)
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>
<ol>
{% for trip in all_trips %}
<ol>
<li><a href="{% url 'trips:activity' trip.slug %}">Trip name: {{ trip.trip_name }}</a></li>
Date: {{ trip.date }}<br>
Planner: {{ trip.planner_name }}<br>
Coplanners:
{% for user in trip.add_coplanner.all %}
{{user.username}}
{% endfor %}<br>
<!-- Co-planner: {{ trip.add_coplanner.all }}<br>-->
</ol>
{% endfor %}
</ol>
<img src="{% static "images/botanical-garden.jpg" %}" alt="Botanical Garden" />
<!-- New line -->
{% endblock %}
</body>
</html>
这是我在 activity.html中的代码:
{% extends 'trips/base.html' %}
{% block title%}
Detail
{% endblock %}
{% block content %}
<h3>Activities for {{trip.trip_name}} </h3>
{% for trip_item in trip.activity_set.all %}
<!--<p>Activity name: {{ trip_item.activity_name }}</p>-->
<ol>
<li>Activity name: {{ trip_item.activity_name }}</li>
Date: {{ trip_item.date }}<br>
Time: {{ trip_item.time }}<br>
Location: {{ trip_item.location }}<br>
Item Type: {{ trip_item.item_type }}<br>
Item No: {{ trip_item.item_number }}<br>
Co-traveller:
{% for user in trip_item.add_cotraveller.all %}
{{user.username}}
{% endfor %}<br>
<!-- Co-traveller: {{ trip_item.add_cotraveller.all }}-->
</ol>
{% endfor %}
{% endblock %}
我必须在 models.py
中进行任何更改吗?
Do I have to make any change in models.py
?
推荐答案
Co-traveller:
{% for user in trip_item.add_cotraveller.all %}
{% if forloop.last %}
{{user.username}}
{% else %}
{{user.username}} ,
{% endif %}
{% endfor %}<br>
您可以使用它.请在,
这篇关于Django:分隔两个用户名的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!