made production changes and added post user foreign key relation

This commit is contained in:
Alexander Schulz
2024-08-30 17:52:52 +02:00
parent 014b3995ae
commit 144263cf8c
132 changed files with 27 additions and 31853 deletions

View File

@ -1,4 +1,5 @@
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
@ -7,6 +8,7 @@ class Post(models.Model):
slug = models.SlugField()
date = models.DateTimeField(auto_now_add=True)
banner = models.ImageField(default='fallback.png', blank=True)
author = models.ForeignKey(User, on_delete=models.CASCADE, default=None) #Kritisch wenn conus medical products are linked to User.objects.get(username='root')company and compony is deleted all products are deleted.
def __str__(self):
return self.title

View File

@ -17,7 +17,7 @@
</a>
</h2>
<p>{{ post.date }}</p>
<p>{{ post.date }} by {{ post.author }}</p>
<p>{{ post.body }}</p>
</article>
{% endfor %}

View File

@ -1,4 +1,4 @@
from django.shortcuts import render
from django.shortcuts import render,redirect
from .models import Post
from django.contrib.auth.decorators import login_required
from . import forms
@ -16,5 +16,13 @@ def post_page(request, slug):
@login_required(login_url="/users/login/")
def post_new(request):
form = forms.CreatePost()
if request.method == "POST":
form = forms.CreatePost(request.POST, request.FILES)
if form.is_valid():
newpost = form.save(commit=False)
newpost.author = request.user
newpost.save()
return redirect('post:list')
else:
form = forms.CreatePost()
return render(request, 'post/post_new.html', {'form': form})