Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向文章django createview foreignkey添加注释_Python_Django_Comments_Django Class Based Views - Fatal编程技术网

Python 向文章django createview foreignkey添加注释

Python 向文章django createview foreignkey添加注释,python,django,comments,django-class-based-views,Python,Django,Comments,Django Class Based Views,我想使用django createview为我的文章添加评论。目前,我可以毫无问题地添加文章。我可以添加评论。但我无法将评论添加到正确的文章中 我已经做了两天了。我真的很想和cbv合作,因为我正在努力学习。如果您能帮助我解释我做错了什么以及如何纠正,我们将不胜感激 models.py class Article(models.Model): title = models.CharField(max_length=255) slug = models.SlugField(max_l

我想使用django createview为我的文章添加评论。目前,我可以毫无问题地添加文章。我可以添加评论。但我无法将评论添加到正确的文章中

我已经做了两天了。我真的很想和cbv合作,因为我正在努力学习。如果您能帮助我解释我做错了什么以及如何纠正,我们将不胜感激

models.py

class Article(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255, null=True, blank=True)
    content = models.TextField(default='')
    author = models.ForeignKey(User)
    featured_image = models.ImageField(upload_to='articles/featured_image', default='')
    created = models.DateTimeField(auto_now_add=True)
    update_date = models.DateTimeField(blank=True, null=True)
    update_author = models.ForeignKey(User, null=True, blank=True,
                                    related_name="+")

    class Meta:
        verbose_name = 'Article'
        verbose_name_plural = 'Articles'
        ordering = ['-created',]


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

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('articles:article-detail', kwargs={'slug': self.slug})


class ArticleComment(models.Model):
    article = models.ForeignKey(Article, 
                                related_name='comments',
                                null=True)
    author = models.ForeignKey(User,
                                null=True)
    text = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = 'Article Comment'
        verbose_name_plural = 'Article Comments'


    def __str__(self):
        return self.text 
views.py

class ArticleDetailView(DetailView):
    model = Article
    context_object_name = 'article'

    def get_context_data(self, **kwargs):
        context = super(ArticleDetailView, self).get_context_data(**kwargs)
        article = Article.objects.all()
        context['sidebar_articles'] = article.order_by('-created')[:15]
        return context


class CreateArticleView(LoginRequiredMixin, CreateView):
    model = Article
    fields = ['title', 'content']

    def form_valid(self, form):
        article = form.save(commit=False)
        #featured_image = form.cleaned_data['featured_image']
        form.instance.author = self.request.user
        form.save()
        return super(CreateArticleView, self).form_valid(form)


class UpdateArticleView(UpdateView):
    model = Article 


class DeleteArticleView(DeleteView):
    model = Article 


class CreateArticleComment(CreateView):
    model = ArticleComment 
    fields = ('text',)

    def post_valid(self, form):
        post = get_object_or_404(Post, 
                                    slug=article.slug)
        article = Article.objects.all()
        articlecomment = form.save(commit=False)
        articlecomment.author = self.request.user
        articlecomment.article = article
        articlecomment.save()
        return redirect('articles:article-detail', slug=article.slug)

    def get_success_url(self):
        return reverse_lazy('articles:article-list')
url.py

urlpatterns = [
    url(r'^$',
        ArticleListView.as_view(),
        name = 'article-list'),


    url(r'^add-article/$',
        CreateArticleView.as_view(),
        name = 'create-article'),



    url(r'^(?P<slug>[-\w]+)/$',
        ArticleDetailView.as_view(),
        name = 'article-detail'),


    url(r'^(?P<slug>[/w-]+)/update/$',
        UpdateArticleView.as_view(),
        name = 'update-article'),


    url(r'^(?P<slug>[/w-]+)/delete/$',
        DeleteArticleView.as_view(),
        name = 'delete-article'),


    url(r'^(?P<slug>[-\w]+)/comment/$',
        CreateArticleComment.as_view(),
        name = 'add-article-comment'),


]
urlpatterns=[
url(r'^$',
ArticleListView.as_view(),
名称=‘文章列表’,
url(r“^add article/$”,
CreateArticleView.as_view(),
名称=‘创建文章’,
url(r'^(?P[-\w]+)/$”,
ArticleDetailView.as_view(),
名称=‘文章详细信息’,
url(r'^(?P[/w-]+)/update/$”,
UpdateArticleView.as_view(),
名称=‘更新文章’,
url(r'^(?P[/w-]+)/delete/$”,
DeleteArticleView.as_view(),
名称=‘删除文章’,
url(r'^(?P[-\w]+)/comment/$”,
CreateArticleComment.as_view(),
名称='添加文章注释',
]
template.html

{% extends 'articles/base.html' %}
{% load static %}
{% load cloudinary %}


{% block page_title %}{{ object.title | title }} | Dad Support{% endblock %}

{% block page_css %}
    <style>
        body {background: rgba(233, 235, 238, 1); color: 50,50,50,1);}
        .header {background: white; overflow: hidden; padding: 10px 0;}
        .navigation {text-align: center;}
        h1 {text-transform: uppercase; font-size: 1.25rem; font-weight: 900; font-family: "Cabin"; padding: 1rem 0.5rem 0.5rem;}
        a {color: rgba(0,0,51,1); font-weight: 900;}
        a:hover {color: rgba(0,0,51,0.8);}
        .fa {display: block;}

        .main {margin: 2rem 0; overflow:hidden;}

        .article-container {background: rgba(255,255,255,1); border-radius: 5px;}
        .article-container p {padding: 0rem 1rem;}
        .first-sidebar {height: 694px;}

        .trending-stories,
        .entrance-wrapper,
        .article-sidebarad {background: white; overflow:auto; border-radius: 5px;}

        .trending-stories,
        .entrance-wrapper, 
        .article-sidebarad {font-size: 0.9rem; padding: 1rem; text-align: center;}

        .entrance-wrapper span {display: block; margin-top: 0.375rem;}

        .spacer {margin: 10px 0;}

        .article-sidebarad img {width: 300px; height: 300px;}

        .trending-stories a {text-transform: capitalize;}

        .trending-stories {text-align: left;}

        .trending-stories-wrapper,
        .article-sidebarad-wrapper {margin: 0 10px;}

        .trending-stories h2,
        .article-sidebarad h2 {font-size: 0.8rem; font-weight: 900; text-transform: uppercase; text-align: left;}
        .trending-stories h3 {font-size: 0.9rem; margin: 0; padding: 0; font-weight: 600;}

    </style>
{% endblock %}

{% block page_content %}
    <div class="row">
        <div class="main">
            <div class="medium-11 small-centered">
                <div class="small-8 medium-8 large-8 columns">
                    <div class="article-container">
                        <h1>{{ article.title }}</h1>
                        {% cloudinary article.featured_image height="469" width="703" crop="scale" %}
                        <p class="text-body">{{ article.content |linebreaks}}</p>
                    </div>
                    <div class="comment-container">
                        <a class="btn btn-default" href="{% url 'articles:add-article-comment' slug=article.slug %}">Add comment</a>
                        {% for comment in article.comments.all %}
                            <div class="comment">
                                <div class="date">{{ comment.created }}</div>
                                <strong>{{ comment.author }}</strong>
                                <p>{{ comment.text|linebreaks }}</p>
                            </div>
                        {% empty %}
                            <p>No comments here yet :(</p>
                        {% endfor %}
                    </div>
                </div>
                <div class="small-4 medium-4 large-4 columns">
                    <div class="article-sidebar">
                        <div class="entrance-wrapper">
                            {% if not user.is_authenticated %}
                            <div class="row">
                                <div class="medium-3 columns">
                                    <p>
                                        <a href="{% url 'articles:create-article' %}">
                                            <i class="fa fa-pencil fa-2x" aria-hidden="true">
                                                <span>Publish</span>
                                            </i>
                                        </a>
                                    </p>
                                </div>
                                <div class="medium-3 columns">
                                    <p>
                                        <a href="{% url 'contact:contact-list' %}">
                                            <i class="fa fa-phone fa-2x" aria-hidden="true">
                                                <span>Contact</span>
                                            </i>
                                        </a>
                                    </p>
                                </div>
                                <div class="medium-3 columns">
                                    <p>
                                        <a href="{% url 'profiles:register' %}">
                                            <i class="fa fa-user-plus fa-2x" aria-hidden="true">
                                                <span>Register</span>
                                            </i>
                                        </a>
                                    </p>
                                </div>
                                <div class="medium-3 columns">
                                    <p>
                                        <a href="{% url 'profiles:login' %}">
                                            <i class="fa fa-sign-in fa-2x" aria-hidden="true">
                                                <span>Sign In</span>
                                            </i>
                                        </a>
                                    </p>
                                </div>
                                {% endif %}
                                {% if user.is_authenticated %}
                                    <div class="row">
                                        <div class="medium-3 columns">
                                            <p>
                                                <a href="{% url 'articles:create-article' %}">
                                                    <i class="fa fa-pencil" aria-hidden="true">
                                                        <span>Publish</span>
                                                    </i>
                                                </a>
                                            </p>
                                        </div>
                                        <div class="medium-3 columns">
                                            <p>
                                                <a href="{% url 'contact:contact-list' %}">
                                                    <i class="fa fa-phone" aria-hidden="true">
                                                        <span>Contact</span>
                                                    </i>
                                                </a>
                                            </p>
                                        </div>
                                        <div class="medium-3 columns">
                                            <p>
                                                <a href="{% url 'profiles:profile-detail' user.username %}">
                                                    <i class="fa fa-user-circle" aria-hidden="true">
                                                        <span>Profile</span>
                                                    </i>
                                                </a>
                                            </p>
                                        </div>
                                        <div class="medium-3 columns">
                                            <p>
                                                <a href="{% url 'profiles:logout' %}">
                                                    <i class="fa fa-sign-out" aria-hidden="true">
                                                        <span>Sign Out</span>
                                                    </i>
                                                </a>
                                            </p>
                                        </div>
                                    </div>
                                    <p>Welcome {{ user.username }} !!!</p>       
                                {% endif %}
                            </div>                          
                        </div>
                        <div class="spacer"></div>
                        <div class="article-sidebarad">
                            <h2>sponsored</h2>
                            <img src="file:///home/absinthe62/Downloads/milkyway.jpg" height="280" width="336">
                        </div>
                        <div class="spacer"></div>
                        <div class="trending-stories">
                            <div class="row">
                                <div class="medium-12">
                                    <div class="trending-stories-wrapper">
                                        <h2>Article Vault</h2>
                                        {% for article in sidebar_articles %}
                                            <h3>
                                                <a href="{% url 'articles:article-detail' article.slug %}">{{ article.title }}</a>
                                            </h3>
                                            <p class="sidebar-body">{{ article.body| truncatewords:13 }}</p>
                                        {% endfor %}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{%extends'articles/base.html%}
{%load static%}
{%load cloudinary%}
{%block page_title%}{{object.title | title}}}Dad支持{%endblock%}
{%block page_css%}
正文{背景:rgba(233235238,1);颜色:50,50,50,1);}
.header{背景:白色;溢出:隐藏;填充:10px 0;}
.navigation{text align:center;}
h1{文本转换:大写;字体大小:1.25rem;字体重量:900;字体系列:“CAB”;填充:1rem 0.5rem 0.5rem;}
a{color:rgba(0,0,51,1);字体大小:900;}
a:hover{color:rgba(0,0,51,0.8);}
.fa{显示:块;}
.main{margin:2rem 0;溢出:隐藏;}
.物品容器{背景:rgba(255255,1);边界半径:5px;}
.article容器p{padding:0rem 1rem;}
.第一个边栏{高度:694px;}
.流行故事,
.入口包装,
.article sidebarad{背景:白色;溢出:自动;边框半径:5px;}
.流行故事,
.入口包装,
.article sidebarad{font size:0.9rem;padding:1rem;text align:center;}
.入口包装跨距{显示:块;页边距顶部:0.375rem;}
.间隔符{边距:10px 0;}
.article sidebarad img{宽度:300px;高度:300px;}
.trending故事{文本转换:大写;}
.trending故事{文本对齐:左;}
.流行故事,
.article sidebarad包装{边距:0 10px;}
.流行故事h2,
.article sidebarad h2{字体大小:0.8rem;字体重量:900;文本转换:大写;文本对齐:左;}
.趋势故事h3{字体大小:0.9rem;边距:0;填充:0;字体重量:600;}
{%endblock%}
{%block page_content%}
{{article.title}}
{%cloudinary article.featured_image height=“469”width=“703”crop=“scale”%%

{{article.content | linebreaks}

{article.comments.all%中的注释为%s} {{comment.created}} {{comment.author}} {{comment.text | linebreaks}}

{%empty%} 这里还没有评论:(

{%endfor%} {%如果不是user.is_身份验证%}

{%endif%} {%if user.u经过身份验证%}

欢迎{{user.username}}!!!

{%endif%}
class CreateArticleComment(CreateView):
    model = ArticleComment 
    fields = ('text',)

    def post_valid(self, form):
        article = get_object_or_404(Article, 
                                    slug=article.slug) # Replaced 'Post' with 'Article'
        articlecomment = form.save(commit=False)
        articlecomment.author = self.request.user
        articlecomment.article = article
        articlecomment.save()
        return redirect('articles:article-detail', slug=article.slug)

    def get_success_url(self):
        return reverse_lazy('articles:article-list')
class CreateArticleComment(CreateView):
    model = ArticleComment 
    fields = ('text',)

def post_valid(self, form):
    post = get_object_or_404(Post, 
                                slug=article.slug)
    article = Article.objects.all()                <----------------------------
    articlecomment = form.save(commit=False)
    articlecomment.author = self.request.user
    articlecomment.article = article
    articlecomment.save()
    return redirect('articles:article-detail', slug=article.slug)

def get_success_url(self):
    return reverse_lazy('articles:article-list')
article = Article.objects.all()
article = get_object_or_404(Article, slug=self.kwargs['slug'])