PythonDjango slug u

2024-03-28 20:37:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个练习pythondjango的网站。我正试着把我大脑中的链接,鼻涕虫连接起来联系人.html分隔html页。手动输入这些对象的URL可以正确地输出站点。但是,中的链接联系人.html我不喜欢翻页。你知道吗

{% extends "base.html" %}
<!DOCTYPE html>
    {% block content %}
    <body>
        <h1>Contact page</h1>
            <div class = "contact">
            {% for contact in contact %}
                <h2><a href="{ % url 'detail' slug = contact.slug %}">{{contact.title}}</a></h2>
            {% endfor %}
            </div>
    </body>
    {% endblock %}

联系人_详细信息.html(中的页href)联系人.html希望使用此html;两个html文件都位于contact/contact/templates中)

{% extends "base.html" %}
<!DOCTYPE html>
    {% block content %}
    <body>
            <div class = "contact">
            <p>{{contact.title}}</p>
            <p>{{contact.body}}</p>
            </div>
    </body>
    {% endblock %}

你知道吗视图.py你知道吗

from django.shortcuts import render
from .models import Contact
from django.http import HttpResponse
def contact(request):
    contact = Contact.objects.all() 
    return render(request, 'contact/templates/contact.html', {'contact': contact})
def contact_details(request, slug):
    contacts = Contact.objects.get(slug=slug)
    return render(request, 'contact/templates/contact_detail.html', {'contact': contacts})

你知道吗网址.py你知道吗

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

urlpatterns = [
    url('^$', views.contact, name="list"),
    url(r'^(?P<slug>[\w-]+)/$', views.contact_details, name = "detail"),
]

你知道吗型号.py你知道吗

from django.db import models

class Contact(models.Model):
    title = models.CharField(max_length=500)
    slug = models.SlugField()
    body = models.TextField()
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title
    def snippet(self):
        return self.body[:50]

你知道吗管理员.py你知道吗

from django.contrib import admin
from .models import Contact
# Register your models here.
admin.site.register(Contact)

错误

Using the URLconf defined in katiesite.urls, Django tried these URL patterns, in this order:

admin/
^contact/ ^$ [name='list']
^contact/ ^(?P<slug>[\w-]+)/$ [name='detail']
^$
The current path, contact/{ % url 'detail' slug = contact1.slug %}, didn't match any of these.

Tags: djangofrompyimportdivurltitlemodels