Django 一对多关系 url

Posted

技术标签:

【中文标题】Django 一对多关系 url【英文标题】:Django One to Many relationship urls 【发布时间】:2018-08-28 00:29:37 【问题描述】:

我使用 django 作为我的网站。但是我有一个关于 blog/urls.py 的问题(我的应用名称是 blog)

我习惯于在 blog/models.py 中建立一对多的关系。

类别 (1 => *) 主题 (1 => *) 文章。

class Category(models.Model):
    name = models.CharField(max_length=200)
    statement = models.TextField()
    slug=models.SlugField()

    page_name = models.ForeignKey('Page', on_delete=models.CASCADE)

    def __str__(self):
        return self.name


class Subject(models.Model):
    name = models.CharField(max_length=200)
    statement = models.TextField()
    slug=models.SlugField()

    category_name = models.ForeignKey('Category', on_delete=models.CASCADE)

    def __str__(self):
        return self.name

class Article(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    slug=models.SlugField()
    text = models.TextField()
    created_date = models.DateTimeField(
        default=timezone.now)
    published_date = models.DateTimeField(
        blank=True, null=True)
    subject_name = models.ForeignKey('Subject', on_delete=models.CASCADE)


    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog:detail', kwargs='id' : self.id)

blog/views.py

def detail(request,article_slug):
article = get_object_or_404(Article, slug=article_slug)
article_list=Article.objects .all()
subject_list = Subject.objects.all()
context=
    'article': article,

    'article_list':article_list,
    'subject_list': subject_list


return render(request, 'blog/detail.html', context)

博客/urls.py

url(r'^(?P<category_slug>[\w-]+)/(?P<subject_slug>[\w-]+)/(?P<article_slug>[\w-]+)/$', views.detail, name='detail'),

我想在点击我文章的链接时看到网址

http://127.0.0.1:8000/myworkandresearch/category_slug/subject_slug/article_slug

blog / urls.py '如何编辑?

【问题讨论】:

对不起,你的问题不清楚。你有什么问题? 嗨,@DanielRoseman。当我点击我文章的链接时,我想查看 url:(127.0.0.1:8000/myworkandresearch/machine_learning/…) numpy 是文章 slug/python_dictionary 是主题 slug/machine_learning 是类别 slug。 【参考方案1】:

我认为你应该有一个详细视图

def detail(request, category_slug, subject_slug, article_slug):
    article = Article.objects.filter(
        slug = article_slug,
        subject__slug = subject_slug,
        subject__category__slug = category_slug
    )
    return render(request, 'blog/detail.html', 'article': article)

您的文章模型需要以下方法

def get_absolute_url(self):
    return reverse('detail', args=[self.slug, self.subject.slug, self.subject.category.slug])

显示您的文章时

<a href="article.get_absolute_url">article.title</a>

您的网址可能如下所示

url(r'^myworkandreseach/(?P<category_slug>[\w-]+)/(?P<subject_slug>[\w-]+)/(?P<article_slug>[\w-]+)/$', views.detail, name='detail'),

一定要导入视图

【讨论】:

嗨,@EmmanuelMtali ı 试试你的答案,但是,我需要在 blog/urls.py 中写什么?【参考方案2】:

编辑我的博客/models.py

class Article(models.Model):
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField(max_length=200)
slug=models.SlugField()
text = models.TextField()
created_date = models.DateTimeField(
    default=timezone.now)
published_date = models.DateTimeField(
    blank=True, null=True)
subject_name = models.ForeignKey('Subject', on_delete=models.CASCADE)


def publish(self):
    self.published_date = timezone.now()
    self.save()

def __str__(self):
    return self.title

def get_absolute_url(self):
     return reverse('blog:detail', args=[self.slug, self.subject.slug, self.subject.category.slug])

blog/views.py

def detail(request, category_slug, subject_slug, article_slug):
article = Article.objects.filter(
    slug = article_slug,
    subject__slug = subject_slug,
    subject__category__slug = category_slug
)

subject_list = Subject.objects.all()
category_list = Category.objects.all()

context = 
    'category_list': category_list,
    'subject_list': subject_list,
    'article': article

return render(request, 'blog/detail.html', context)

博客/urls.py

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

app_name = 'blog'
urlpatterns = [
    url(r'^$', views.myworkandresearch, name='myworkandresearch'),
    url(r'(?P<category_slug>[\w-]+)/$', views.subjects, name='subjects'),
    url(r'^(?P<category_slug>[\w-]+)/(?P<subject_slug>[\w-]+)/(?P<article_slug>[\w-]+)/$', views.detail, name='detail'),
]

subjects.html

% for article in subject.article_set.all %
                <ul class="sidebar-items">
                  <li><a href="article.get_absolute_url">article.title</a></li>
                </ul>
                % endfor %

[错误继续。][2]

找不到页面 (404) 请求方法:GET 请求 URL:http://127.0.0.1:8000/myworkandresearch/machine_learning/python_dictionary/numpy/ 提出者:blog.views.subjects 没有与给定查询匹配的类别。

您看到此错误是因为您的 Django 中有 DEBUG = True 设置文件。将其更改为 False,Django 将显示 标准 404 页面。

【讨论】:

以上是关于Django 一对多关系 url的主要内容,如果未能解决你的问题,请参考以下文章

Django 无法确定使用一对一关系链接一对多的查询集

如何使用 Django 查询从一对多关系中获取数据

Django - 以一对多关系更改相关对象的值

Django Postgres ArrayField 与一对多关系

26.Django实现表关系(一对多,一对一,多对多))

26.Django实现表关系(一对多,一对一,多对多))