ZHARP
ZHARP

Reputation: 132

On my Blog - matching query does not exists

I have tried to learn like this. And try to develop my blog and add SLUG to the function But it is not possible to add new posts at all Do you have a solution or a way to make it work?

https://tutorial.djangogirls.org/en/django_forms/

on Models like this.

class Post(models.Model):
id = models.AutoField
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
content_images = models.ImageField(default='Choose Your Images')
title = models.CharField(max_length=200,unique=True)
content = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
post_viewcount = models.PositiveIntegerField(default=0)
slug = models.SlugField(max_length=200, default='Enter SEO URL')
status = models.IntegerField(choices=STATUS , default=0)

and views.py like this

def create_post(request):
form = Createcontent
return render(request, 'blog/post_creator.html', {'form': form})

and urls.py like this

urlpatterns = [
path('', views.post_list, name='post_list'),
path('content/<slug:slug>/', views.post_detail, name='post_detail'),
path('content/createcontent/', views.create_post, name='create_post'),

and html file.

   {% extends 'blog/base.html' %} {% block content %}
<h2>New post</h2>
<form method="POST" class="post-form">{% csrf_token %} {{ form.as_p }}
    <button type="submit" class="save btn btn-default">Save</button>
</form>
{% endblock %}

Upvotes: 3

Views: 44

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476584

For your 'create_post' path, the <slug:slug> will first capture this, since createcontent is a valid slug as well. Therefore you should alter the urlpatterns. You can swap the two:

urlpatterns = [
    path('content/createcontent/', views.create_post, name='create_post'),
    path('content/<slug:slug>/', views.post_detail, name='post_detail'),
]

so now createcontent will be matched first, but still this is not a good solution, since now if your article has as slug createcontent, you can not display it. Likely it is better to make non-overlapping paths. For example:

urlpatterns = [
    path('content/createcontent/', views.create_post, name='create_post'),
    path('post/<slug:slug>/', views.post_detail, name='post_detail'),
]

here regardless what the value for slug is, it will never overlap with the content/createcontent path.

You of course still need to finish the view itself, since right now, it will not handle a POST request properly.

Upvotes: 3

Related Questions