Sunday, April 28, 2019

Specific Blog Post not showing whole post after clicking link

I have a personal_blog app that links to a full view of the post the user clicked on. I had it working but had to change the code so only the author could see it and somehow that ruined the fix. it links fine to the post, shows post id in the url (i.e 127.0.0.1:8000/personal_blog/1/ ) but no post info. I've run out of research on this, I keep getting the same links and alot of them have depricated code.

Thank you for the help


<!-- 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()
# Create your models here.
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):
posts = PersonalBlogPost.objects.get(pk=request.post_id)
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'),


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cc02a4e5-920a-49df-baa9-7ec8c25ee3a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment