0

First problem:

When i create new post i see this message (Reverse for 'details' with keyword arguments '{'pk': 1, 'slug': ''}' not found. 1 pattern(s) tried: (['post/(?P<pk>\\d+)(?:/(?P<slug>[\\w\\d-]+))?/$'])

Secound problem:

When i call get_absolute_url in readmore botton i see this message ('PostCreate' object has no attribute 'get_absolute_url')

tow days i try to fix this problem I tried all the solutions but im failed

here is my code

views.py

class PostCreate(CreateView):
    model = Post
    template_name = "posts/create.html"
    query_pk_and_slug = True
    fields = ['title','body','category']
    def form_valid(self, form):
        form = form.save(commit=False)
        form.user = self.request.user
        return super(PostCreate, self).form_valid(form)

    def get_success_url(self):
        return self.get_absolute_url
    
    

models.py

class Category(models.Model):
    name = models.CharField(max_length=100, blank=True, null=True, default="")

    def __str__(self):
        return self.name

class Post(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=100, blank=False, null=False, default="")
    body = models.TextField(max_length=1000,blank=False, null=False, default="")
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    slug  = models.SlugField(blank=True,default="-",unique=False)
    updated_at = models.DateTimeField(auto_now_add=False, auto_now=True)
    created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
   
    def __str__(self):
        return self.title

    def get_absolute_url(self,):
        return reverse('post:details', kwargs={'pk': self.id, 'slug': self.slug})
    
    

urls.py

from django.conf.urls import url
from .views import (PostList, PostDetail, PostCreate, PostUpdate, ArticleDetailRedirect)
from django.urls import reverse
urlpatterns = [
     url(r'^$', PostList.as_view(), name="index"),
     url(r'^create/$', PostCreate.as_view(), name="create" ),
     url(r'^(?P<pk>[0-9]+)/update/$', PostUpdate.as_view(), ),
     url(r'^(?P<pk>\d+)/$', PostDetail.as_view()),
     url(r'^(?P<pk>\d+)(?:/(?P<slug>[\w\d-]+))?/$', PostDetail.as_view(), name = 'details'),
     url(r'^(?P<pk>\d+)(?:/(?P<slug>[\w\d-]+))?/$', ArticleDetailRedirect.as_view(), name = 'article_details'),

index.html

<a href="{{ list.get_absolute_url }}">
   <button type="button" class="btn btn-primary">
     <span class="btn btn-google">Read More
      <i class="fa fa-chevron-right"></i>
     </span>
  </button>
</a>

thanks

Community
  • 1
  • 1
  • 6
    Please write one problem per question only. – Klaus D. Jun 08 '17 at 09:05
  • 1
    Well, you're missing the slug for the first problem, and the error is correct for the second, your `PostCreate` doesn't have a get_absolute_url function. Please read [ask] and try to create a [mcve] – Sayse Jun 08 '17 at 09:31
  • 1
    Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](https://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – Sayse Jun 08 '17 at 09:31
  • im sorry about that sir my english is bad and im new in this website and i have read some informations before im ask ... anyway so the reverse its removerd from django 1.10 im using know @permalink but the problem is still when i create post (No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model.) – steven_ Jun 08 '17 at 10:42

0 Answers0