Почему не отображается форма сообщения? django
Есть 3 приложения: "публикация", "app_test" - основное, "usercreatepost", тоже пользователи с авторизацией, но там все нормально.
публикация приложения:
views.py:
`
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse, HttpResponseNotFound, Http404
from publication.models import Userpublication
from django.urls import reverse
from publication.forms import PostEditForm
from usercreatepost.forms import PostForm
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.views import LoginView
from django.contrib.auth.decorators import login_required
from django.contrib import messages
`
def indexs(request):
posted = Userpublication.published.filter(is_published=1)
return render(request, 'publication/post.html', {'post_lists': posted})
def show_post(request, post_slug, *args, **kwargs):
post = get_object_or_404(Userpublication, slug=post_slug)
data = {
'user': post.user,
'content': post.content,
'post': post,
}
return render(request, 'publication/post_user.html', data)
@login_required
def edit_post(request, post_slug):
post = get_object_or_404(Userpublication, slug=post_slug)
if request.user != post.author:
messages.error(request, 'Вы не можете редактировать этот пост')
return redirect('feeds', post_slug=post.slug)
if request.method == 'POST':
form = PostEditForm(request.POST, instance=post)
if form.is_valid():
form.save()
messages.success(request, 'Post success') # flash-message
return redirect('feeds', post_slug=post.slug) # redirect
else:
form = PostEditForm(instance=post)
return render(request, 'publication/edit_post.html', {'form': form})
def get_edit_url(self):
return reverse('edit_post', kwargs={'post_slug': self.slug})
def post_detail(request, post_slug):
post = get_object_or_404(Userpublication, slug=post_slug)
return render(request, 'publication/post_user.html', {'post': post})
urls.py:
from django.urls import path
from . import views
from .views import edit_post
from usercreatepost.views import create_post
urlpatterns = [
path('create/', create_post, name='create_posts'),
path('<slug:post_slug>/', edit_post, name='feeds'),
path('posts/<slug:post_slug>/', views.show_post, name='poserts'),
path('post/<slug:post_slug>/', views.post_detail, name='post_detail'),
path('edit/<slug:post_slug>/', edit_post, name='edit_post'),
]
models.py:
from django.db import models
from autoslug import AutoSlugField
from django.urls import reverse
from django.contrib.auth.models import User
class PublishedManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_published=1).order_by('-time_create')
class Userpublication(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
slug = AutoSlugField(max_length=255, unique=True, populate_from='content')
content = models.TextField('', blank=True)
time_create = models.DateTimeField('time_create ', auto_now_add=True)
time_update = models.DateTimeField('time_update ', auto_now=True)
is_published = models.BooleanField('is_published ', default=True)
objects = models.Manager()
published = PublishedManager()
def __str__(self):
return self.slug
class Meta:
ordering = ['-time_create']
indexes = [
models.Index(fields=['-time_create'])
]
class Meta:
verbose_name = 'Post'
verbose_name_plural = 'Posts'
def get_absolute_url(self):
return reverse('post_detail', kwargs={'post_slug': self.slug})
def get_edit_url(self):
return reverse('edit_post', kwargs={'post_slug': self.slug})
forms.py:
from django import forms
from .models import Userpublication
class PostEditForm(forms.ModelForm):
class Meta:
model = Userpublication
fields = ['content']
приложение usercreatepost;
views.py:
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib import messages
from .forms import PostForm
from publication.models import Userpublication
def create_post(request):
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
messages.success(request, 'Post successfully created!')
return redirect('home')
else:
form = PostForm()
return render(request, 'usercreatepost/create_post.html', {'form': form})
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.create_post, name='create_post'),
]
Формы:
from django import forms
from publication.models import Userpublication
class PostForm(forms.ModelForm):
class Meta:
model = Userpublication
fields = ['content']
приложение app_test;
Просмотров:
from django.shortcuts import render
from django.views.generic.base import View
from django.contrib.auth.decorators import login_required
from publication.models import Userpublication
@login_required
def indexs(request):
posted = Userpublication.published.all()
return render(request, 'app_test/home.html', {'post_lists': posted})
Урлы:
from django.urls import path
from . import views
from usercreatepost.views import create_post
urlpatterns = [
path('feed/', views.indexs, name='home'),
]
Шаблон: home и layout.
При создании поста с помощью url 'create/' форма отображается и пост добавляется. А когда я добавляю его в шаблон home основного приложения app_test, форма перестает отображаться, отображается только кнопка "создать пост". В чем проблема?
Я вставил форму в приложение app_test, но результата нет