点击帖子标题后未显示特定帖子

时间:2019-04-28 13:59:10

标签: django-models django-templates django-views django-urls django-2.2

已修复,对于这个问题如此明显,我深表歉意,对于帖子如此凌乱,我深感抱歉,我每天在该项目上花费了18个小时,而且已经花光了,我会将该修复程序放在下面的代码中,并在其旁边加上“ <== Changed”,仅需要更改views.py(以防有人需要)可忽略所有其他编辑内容

单击标题后,我无法显示特定的帖子,但是该帖子没有显示,并且unicode(源html?)仅显示了我base.html中的空div类({块内容})重定向到单个帖子页面(视图名称为view_post)后不显示。我需要更改什么?

我正在制作一个个人博客页面,人们可以在其中看到任何帖子的标题,并且当他们单击它时,它会将他们带到整个帖子。我尝试向模型中添加一个子字段,并在view_post函数中使用它(我已经重写了很多次),尝试在引用该视图的url中使用?P。尝试过DetailTemplateView。我通过在视图中使用post_id使它工作一次,但是我弄乱了该代码,所以现在我又迷路了。尝试更改HTML中的变量引用(它在页面上显示为空标签,并且是模板中的唯一单词。)尝试了render_to_response,尝试使用pk作为view_post视图的参数。我似乎已经尝试了所有内容,经过三天的研究,我一直在使用不推荐使用的内容获得相同的链接。

<!-- personal_blog/personal_base.html-->
{% extends 'personal_blog/personal_base.html' %}

{% block content %}
        {% for post in posts %}
        <h5>{{ post.date|date:"Y-m-d" }}<a href="{{ post.id }}">{{ post.author }}
                {{ post.title }}</a></h5>
        {% endfor %}
{% endblock %}
<!--personal_blog/post.html-->
{% extends "personal_blog/personal_base.html" %}

{% block content %}

    <h3>{{ post.title }}</h3>
    <h6>on {{ post.published_date }}</h6>
    <p>{{ post.text|linebreaks }}</p>

{% endblock %}
# personal_blog/models.py
from django.conf import settings
from django.db import models
from django.utils import timezone
from django.utils.http import urlquote
from django.utils.translation import ugettext_lazy as _
from django.core.mail import send_mail
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.contrib.auth.models import BaseUserManager
from django.contrib.auth import get_user_model
from datetime import datetime
User = get_user_model()

class PersonalBlogPost(models.Model): 
    objects = models.Manager() # <==Could not call PersonalBlogPost.objects on anything unless I did this. 
    author = models.ForeignKey(User, related_name='personal_blog_poster', on_delete=models.CASCADE, null=True)
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(auto_now=True, null=True)#, blank=True) 
    published_date = models.DateTimeField(auto_now=True, null=True)#blank=True)
    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title
# public_posts/views.py

from django.shortcuts import render
from django.utils import timezone
from .models import PersonalBlogPost
from django.urls import reverse_lazy
from django.views import generic
from django.http import HttpResponseRedirect, HttpResponseNotFound
from django.shortcuts import render, redirect
from . import forms
from django.forms import ModelForm as Form
from datetime import datetime

def personal_blog_post_list(request):

    posts = PersonalBlogPost.objects.filter(published_date__lte=datetime.now()).order_by('-published_date')
    return render(request, 'personal_blog/personal_blog.html', {'posts': posts})

def view_post(request):
    author = request.user <==added

   #posts = PersonalBlogPost.objects.get(pk=request.post_id) <==This was wrong

    posts = PersonalBlogPost.objects.filter(author_id=author) <==Changed to this

    return render(request, 'personal_blog/post.html', {'posts': posts})

def new_personal_post(request):
    if request.method == 'POST':
        form = forms.PersonalBlogPostForm(request.POST)
        if form.is_valid():
            personal_post = form.save(commit=False)
            personal_post.author = request.user
            personal_post.save()
            return redirect('blog_personal')
    else:
        form = forms.PersonalBlogPostForm()
    return render(request, 'personal_blog/new_post.html', {'form': form})
# personal_blog/urls.py

from django.conf.urls import url, include
from django.urls import path
from django.views.generic import ListView, DetailView, TemplateView
from .models import PersonalBlogPost
from . import views
urlpatterns = [
        path('', views.personal_blog_post_list, name='personal_blog'),
        url(r'^personal_blog/', views.new_personal_post, name='new_post'), 
        url(r'^(?P<author>\w)/$', ListView.as_view(model = PersonalBlogPost,
                template_name = 'personal_blog/personal_blog.html')),
        url(r'^',views.view_post, name='view_post'),
]

预期结果是单击您的帖子标题,然后将其重定向到一个页面,以便能够阅读整个帖子。 实际结果是,您已成功重定向到http://127.0.0.1:8000/personal_blog/1/(或其他帖子ID),但未显示任何内容 编辑 因此,我尝试在页面上发布新的东西,但是由于我给某个东西命名错误而引发了错误,现在我不是将我带到空白页面,而是告诉我“ WSGIRequest”对象没有属性“ id”“它正在谈论:posts = PersonalBlogPost.objects.get(pk=request.id) 在view_post视图中,这是在杀死我哈哈哈阿普(Haaaaalp)水解酶。 (当我了解更多信息时,可以打赌,我将遍及这些委员会试图帮助人们,因为这太可怕了,为期四天的研究,无可奉告。) 编辑2 我将post_view更改为

def view_post(request):
    posts = PersonalBlogPost.objects
    return render(request, 'personal_blog/post.html', {'posts': posts})

现在,它重定向到页面并显示post.html模板中的唯一工作,并且显示源HTML(空格显示标签,抱歉,我是新来的。)

< h3>< /h3>  
< h6>on < /h6>
< p>< /p>

我应该在哪里发布信息,我也将post.html中的变量更改为{{posts.variable}}(尝试了posts.variable [post。或posts。没有运气))

当我点击要查看的帖子时,这在我的终端上出现

[28/Apr/2019 11:22:07] "GET /personal_blog/8 HTTP/1.1" 301 0
[28/Apr/2019 11:22:07] "GET /personal_blog/8/ HTTP/1.1" 200 2106

第一个是绿色,最后一个是白色,应该这样做吗?

0 个答案:

没有答案