Почему не открывается страница редактирования сообщения, django?
Код, который корректно работает при редактировании поста. Но после добавления модели AbstractUser что-то пошло не так.
application publication: views.py:
@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, 'You cannot edit this post')
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, 'The post has been successfully edited!') # Setting flash message
return redirect('feeds', post_slug=post.slug) # Redirect to the post view page
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}
publication application forms.py:
from django import forms
from .models import Userpublication
class PostEditForm(forms.ModelForm):
class Meta:
model = Userpublication
fields = ['content']
публикация приложения models.py:
from django.db import models
from autoslug import AutoSlugField #AutoSlugField
from django.urls import reverse
from django.contrib.auth.models import User
from django.conf import settings
from user.models import ProfileUser # ProfileUser
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(
ProfileUser,
on_delete=models.CASCADE,
related_name='publications',
blank=True,
null=True,
)
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})
Публичное приложение 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_post/<slug:post_slug>/', views.edit_post, name='edit_post'),
]
редактировать ссылку:
{% if request.user == post.author %}
<a href="{{ post.get_edit_url }}" class="post_user_edit">Edit</a>
{% endif %}
Этот код работает правильно, но после того, как я добавил модель AbstractUser, ссылка не работает, а вместо нее открывается правильная ссылка для slug, но с ошибкой:
Страница недоступна. Localhost перенаправлял слишком много раз.
изменил его на такой:
<a href="{% url 'edit_post' post_slug=post.slug %}" class="post_user_edit">Edit</a>
user application models.py:
from django.db import models
from django.contrib.auth.models import AbstractUser
class ProfileUser(AbstractUser):
bio = models.TextField(max_length=200, blank=True, null=True)
first_name = models.CharField(max_length=40, blank=True, null=True)
last_name = models.CharField(max_length=50, blank=True, null=True)
birth_date = models.DateField(blank=True, null=True)
verified = models.BooleanField(default=False)
registration_date = models.DateTimeField(auto_now_add=True)
avatar = models.ImageField(upload_to='avatars/', blank=True, null=True)
is_active = models.BooleanField(default=True)
author = models.ForeignKey(
'publication.Userpublication',
on_delete=models.SET_NULL,
related_name='author_profile',
blank=True,
null=True,
)
class Meta(AbstractUser.Meta):
pass
пользовательское приложение views.py:
@login_required
def user_profile(request):
user = request.user
user_data = {
'username': user.username,
'first_name': user.first_name,
'last_name': user.last_name,
'email': user.email,
'bio': user.bio,
'birth_date': user.birth_date,
}
return render(request, 'user/profile.html', {'user_data': user_data})
изменил его на такой:
<a href="{% url 'edit_post' post_slug=post.slug %}" class="post_user_edit">Edir</a>
не работает