Django NoReverseMatch Error during Template Rendering - python

I am setting up a simple blog site with Django, and run into this error when trying to link to a page that allows users to edit existing blog posts.
Reverse for 'edit_post' with arguments '('',)' not found. 1 pattern(s) tried: ['edit_post/(?P<title_id>[0-9]+)/$']
If I understand this error correctly, it means that Django can't find a urlpattern that matches the url being requested. To my eyes I have the urlpattern set up correctly, but I still get the error.
The link in question appears on the text.html template, which is the template that displays the text of a particular blog post.
Here is the relevant code:
urls.py
"""Defines URL patterns for blogs."""
from django.urls import path
from . import views
app_name = 'blogs'
urlpatterns = [
# Home page, shows all posts in chronological order.
path('', views.index, name='index'),
# A page to show the text of a specific post.
path('text/<int:title_id>/', views.text, name='text'),
# Page for adding a new post.
path('new_post/', views.new_post, name='new_post'),
# Page for editing a post.
path('edit_post/<int:title_id>/', views.edit_post, name='edit_post'),
]
views.py
from django.shortcuts import render, redirect
from .models import BlogPost
from .forms import BlogPostForm
def index(request):
"""The home page for blogs, shows all posts."""
titles = BlogPost.objects.order_by('date_added')
context = {'titles': titles}
return render(request, 'blogs/index.html', context)
def text(request, title_id):
"""Show a single post title and its text."""
title = BlogPost.objects.get(id=title_id)
text = title.text
context = {'title': title, 'text': text}
return render(request, 'blogs/text.html', context)
def new_post(request):
"""Add a new post."""
if request.method != 'POST':
# No data submitted; create a new form.
form = BlogPostForm()
else:
# POST data submitted; process data.
form = BlogPostForm(data=request.POST)
if form.is_valid():
new_post = form.save(commit=False)
new_post.save()
return redirect('blogs:index')
# Display a blank or invalid form.
context = {'form': form}
return render(request, 'blogs/new_post.html', context)
def edit_post(request, title_id):
"""Edit an existing post."""
post = BlogPost.objects.get(id=title_id)
if request.method != 'POST':
# Initial request: pre-fill form with the current post.
form = BlogPostForm(instance=post)
else:
# Post data submitted; process data.
form = BlogPostForm(instance=post, data=request.POST)
if form.is_valid():
form.save()
return redirect('blogs:index')
context = {'post': post, 'form': form}
return render(request, 'blogs/edit_post.html', context)
index.html (this is the homepage for the blog)
{% extends "blogs/base.html" %}
{% block content %}
<p>Blog is a generic site for a blogger to post content for their audience.</p>
<p>Posts</p>
<ul>
{% for title in titles %}
<li>
{{ title }}
</li>
{% empty %}
<li>No posts have been added yet.</li>
{% endfor %}
</ul>
Create a new post
{% endblock content %}
text.html (this page displays the text content of a particular post, and also the link to edit the post)
{% extends "blogs/base.html" %}
{% block content %}
<p>Title: {{ title }}</p>
<p>{{ text }}</p>
Edit post
{% endblock content %}
edit_post.html (this page should display the existing post and allow it to be edited)
{% extends "blogs/base.html" %}
{% block content %}
<p>Edit post:</p>
<p>Title:</p>
<form action="{% url 'blogs:edit_post' title.id %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Save changes</button>
</form>
{% endblock content %}
How the edit_post function in views.py should work (in theory) is to create an instance based upon the post's title id, and then allowing the user to edit it and save changes.
I'm not sure where I'm going wrong here and any suggestions are greatly appreciated.

The name of the post object you pass to the template, is not title, but post:
{% extends "blogs/base.html" %}
{% block content %}
<p>Edit post:</p>
<p>Title:</p>
<form action="{% url 'blogs:edit_post' post.pk %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Save changes</button>
</form>
{% endblock content %}
If you use title.id, it will not find that variable, and thus this will be resolved to the empty string. If you use post.id, or post.pk, it will resolve to the .id field, or primary key of the post object.

Related

Reverse for 'edit_blog_post' with arguments '('',)' not found

I am trying to create a way to edit individual blog posts from their individual html. Here are the relevant files and trace back. I am somewhat understanding that the issue lies in blog_post.id being due to the fact that blog_post has not carried over from the for loop on blog_posts.html. I have read up on others having this issue and they all structured their pages to have the edit button being inside the original for loop, which makes sense in hindsight. BUT now that I have run into this issue, I'm determined to understand how I can solve it without going back and restructuring my pages to align with the others I saw.
urls.py
from django.urls import path
from . import views
app_name = 'blogs'
urlpatterns = [
# Home page
path('', views.index, name='index'),
path('blog_posts/', views.blog_posts, name='blog_posts'),
path('blog_posts/<int:blog_post_id>/', views.blog_post, name='blog_post'),
path('new_blog_post/', views.new_blog_post, name='new_blog_post'),
path('edit_blog_post/<int:blog_post_id>/', views.edit_blog_post, name='edit_blog_post'),
]
views.py
from .models import BlogPost
from .forms import BlogPostForm
def index(request):
"""Home page for Blog."""
return render(request, 'blogs/index.html')
def blog_posts(request):
"""Show all Blog Posts."""
blog_posts = BlogPost.objects.order_by('date_added')
context = {'blog_posts': blog_posts}
return render(request, 'blogs/blog_posts.html', context)
def blog_post(request, blog_post_id):
"""Show details of an individual blog post."""
blog_post = BlogPost.objects.get(id=blog_post_id)
title = blog_post.title
id = blog_post_id
date = blog_post.date_added
text = blog_post.text
context = {'title': title, 'text': text, 'date': date}
return render(request, 'blogs/blog_post.html', context)
def new_blog_post(request):
"""Add a new blog post"""
if request.method != 'POST':
# No data submitted, create a blank form.
form = BlogPostForm()
else:
# POST data submitted, process data.
form = BlogPostForm(data=request.POST)
if form.is_valid():
form.save()
return redirect('blogs:blog_posts')
# Display a blank or invalid form.
context = {'form': form}
return render(request, 'blogs/new_blog_post.html', context)
def edit_blog_post(request, blog_post_id):
"""Edit an existing blog post's title or text."""
blog_post = BlogPost.objects.get(id=blog_post_id)
if request.method != 'POST':
# Initial request, prefill with the current data.
form = BlogPostForm(instance=blog_post)
else:
# POST data submitted; process new data.
form = BlogPostForm(instance=blog_post, data=request.POST)
if form.is_valid():
form.save()
return redirect('blogs:blog_post', blog_post_id=blog_post.id)
context = {'blog_post': blog_post, 'form': form}
return render(request, 'blogs/edit_blog_post.html', context)
models.py
from django.db import models
class BlogPost(models.Model):
"""A post the user is posting on their blog."""
title = models.CharField(max_length=200)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""Return a string representation of the model"""
return f"{self.title.title()}"
blog_posts.html
{% extends 'blogs/base.html' %}
{% block content %}
<p>Blog Posts</p>
<ul>
{% for blog_post in blog_posts %}
<li>
{{ blog_post }}
</li>
{% empty %}
<li>No posts have been made yet.</li>
{% endfor %}
</ul>
Add a new blog post
{% endblock content %}
blog_post.html
{% extends 'blogs/base.html' %}
{% block content %}
<p>Blog Post: {{ title }}</p>
<p>Entry:</p>
<p>{{ text }}</p>
<p>{{ date }}</p>
<p>
Edit Blog Post
</p>
{% endblock content %}
edit_blog_post.html
{% extends "blogs/base.html" %}
{% block content %}
<p>
{{ blog_post }}
</p>
<p>Edit Blog Post</p>
<form action="{% url 'blogs:edit_blog_post' blog_post.id %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Save Changes</button>
</form>
{% endblock content %}
Reverse for 'edit_blog_post' with arguments '('',)' not found. 1 pattern(s) tried: ['edit_blog_post/(?P<blog_post_id>[0-9]+)/\Z']
3 {% block content %}
4
5 <p>Blog Post: {{ title }}</p>
6
7 <p>Entry:</p>
8
9 <p>{{ text }}</p>
10 <p>{{ date }}</p>
11
12 <p>
13 Edit Blog Post
14 </p>
15
16 {% endblock content %}
If I've read the question correctly, You're getting the error becuase you are not providing the necessary ID to the URL construction part of your template.
You're separating out the elements (date, content etc) to send to the template, but not passing the ID at the same time. You could send the ID in as a separate context variable, but that's extra typing for no real reward.
It's easiest to pass in the post itself via context and refer to its attributes in the template - I think it makes it easier to read also. That way the ID is there when you need to contruct the edit link, and if you change the model to possess extra fields, you don't need to convert and add to the context as the whole post is already there.
views.py
def blog_post(request, blog_post_id):
"""Show details of an individual blog post."""
blog_post = BlogPost.objects.get(id=blog_post_id) #this is all we need
context = {"blog_post_context": blog_post}
return render(request, 'blogs/blog_post.html', context)
blog_post.html
{% extends 'blogs/base.html' %}
{% block content %}
<p>Blog Post: {{ blog_post_context.title }}</p>
<p>Entry:</p>
<p>{{ blog_post_context.text }}</p>
<p>{{ blog_post_context.date }}</p>
<p>
Edit Blog Post
</p>
{% endblock content %}
If that all works, look into using get_object_or_404 rather than Post.objects.get for some additional robustness.
I assume you got the error when you try visiting the blog_post.html page. If I'm correct, then here's an approach you could take...
In your views.py
def blog_post(request, blog_post_id):
"""Show details of an individual blog post."""
# blog_post = BlogPost.objects.get(id=blog_post_id)
blog_post = get_object_or_404(id=blog_post_id) # Recommended
# Commented lines below are somewhat not necessary...
# title = blog_post.title
# id = blog_post_id
# date = blog_post.date_added
# text = blog_post.text
context = {'blog_post': blog_post}
return render(request, 'blogs/blog_post.html', context)
edit_blog_post.html is expecting an object called blog_post to be able to access the blog_post.id for {% url 'blogs:edit_blog_post' blog_post.id %}.
Now within the edit_blog_post.html file.
{% block content %}
<p>Blog Post: {{ blog_post.title }}</p>
<p>Entry:</p>
<p>{{ blog_post.text }}</p>
<p>{{ blog_post.date_added }}</p>
<p>
Edit Blog Post
</p>
{% endblock content %}

python crash course 19-1 edit posts not working

This is the error I get when clicking on Edit post under any one of the posts. Would appreciate any help as all this django stuff is confusing me but trying my best to learn. My new post function works and clicking blog/posts to go to the overview page for the blog or to look at all the posts works as well.
NoReverseMatch at /edit_post/1/
Reverse for 'posts' with arguments '(1,)' not found. 1 pattern(s) tried: ['posts/$']
Error during template rendering
In template C:\Users\seng\Desktop\Python projects\c19\nineteen_one\blogs\templates\blogs\base.html, error at line 0
urls.py
"""Defines url paterns for blogs"""
from django.urls import path
from . import views
app_name = 'blogs'
urlpatterns =[
#Home page
path('', views.index, name='index'),
# Page that shows all posts/
path('posts/', views.posts, name='posts'),
#Page for adding a new blogpost
path('new_post/', views.new_post, name='new_post'),
#Page for editing a post
#maybe remove the id?
path('edit_post/<int:post_id>/', views.edit_post, name='edit_post'),
]
views.py
from django.shortcuts import render, redirect
from .models import BlogPost
from .forms import BlogPostForm
# Create your views here.
def index(request):
"""The home page for blogs"""
return render(request, 'blogs/index.html')
def posts(request):
"""Show all blogposts"""
posts = BlogPost.objects.order_by('date_added')
context = {'posts': posts}
return render(request, 'blogs/posts.html', context)
def new_post(request):
"""Add a new blogpost"""
if request.method != 'POST':
#No data submitted; create a blank form.
form = BlogPostForm()
else:
#POST data submitted, process data
form = BlogPostForm(data=request.POST)
if form.is_valid():
form.save()
return redirect('blogs:posts')
#Display a blank or invalid form
context = {'form': form}
return render(request, 'blogs/new_post.html', context)
def edit_post(request, post_id):
"""Edit existing post"""
post = BlogPost.objects.get(id=post_id)
if request.method != "POST":
#Initial request, pre-fill form with the current post
form = BlogPostForm(instance=post)
else:
#Post data submitted, process data
form = BlogPostForm(instance=post, data=request.POST)
if form.is_valid():
form.save()
return redirect('blogs:posts', post_id=post.id)
context = {'post':post, 'form':form}
return render(request, 'blogs/edit_post.html', context)
forms.py
from django import forms
from .models import BlogPost
class BlogPostForm(forms.ModelForm):
class Meta:
model = BlogPost
fields = ['text', 'title']
labels = {'text':'This is the text box', 'title' :"Title here"}
edit_post.html
{% extends "blogs/base.html" %}
{% block content %}
<p>{{ post }}</p>
<p>Edit post:</p>
<form action="{% url 'blogs:edit_post' post.id %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Save changes</button>
</form>
{% endblock content %}
posts.html
{% extends "blogs/base.html" %}
{% block content %}
<p>Posts</p>
<ul>
{% for post in posts %}
<li>
<p>{{ post }}</p>
<p>
Edit post
</p>
</li>
{% empty %}
<li>No posts have been added yet.</li>
{% endfor %}
</ul>
Add a new post
{% endblock content %}
new_post.html
{% extends "blogs/base.html" %}
{% block content %}
<p>Add a new post:</p>
<form action="{% url 'blogs:new_post' %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Add post</button>
</form>
{% endblock content %}
The issue is with this line in edit_post.html:
<p>{{ post }}</p>
If you are editing the post with id 1, then this link is to the url /posts/1. But that has no match in your urls.py file.
Either you need to remove the post.id parameter from the link, or create a view and a corresponding path in urls.py for this link.

Reverse for 'new_entry' with arguments '('',)' not found. 1 pattern(s) tried: ['new_entry/(?P<topic_id>[0-9]+)/$']

I'm new learning Django and I want a study tracker web app with Django. The error came up when I was creating the function and template for new entries - this will allow a user write a detailed note about a topic they are currently learning about but whenever I run the template I get an error message:
Reverse for 'new_entry' with arguments '('',)' not found. 1 pattern(s) tried: ['new_entry/(?P[0-9]+)/$'].
I've tried looking at the latest new_entry() function I wrote and tweak the topic variable name but the did not solve the issue. I also cross checked my url paths for any misspelling or whitespaces but there isn't. Here are my project files.
urls.py
from django.urls import path
from . import views
app_name = 'django_apps'
urlpatterns = [
# Home page
path('', views.index, name='index'),
# Page that shows all topics.
path('topics/', views.topics, name='topics'),
# Detail page for a single topic.
path('topics/<int:topic_id>/', views.topic, name='topic'),
# Page for adding a new topic.
path('new_topic/', views.new_topic, name='new_topic'),
# Page for adding a new entry.
path('new_entry/<int:topic_id>/', views.new_entry, name='new_entry'),
]
views.py:
from django.shortcuts import render, redirect
from .models import Topic
from .forms import TopicForm, EntryForm
# Create your views here.
def index(request):
"""The home page for django app."""
return render(request, 'django_apps/index.html')
def topics(request):
"""Show all topic"""
topics_list = Topic.objects.order_by('id')
context = {'topics_list': topics_list}
return render(request, 'django_apps/topics.html', context)
def topic(request, topic_id):
"""Get topic and all entries associated with it."""
topic_list = Topic.objects.get(id=topic_id)
entries = topic_list.entry_set.order_by('-date_added')
context = {'topic_list': topic_list, 'entries': entries}
return render(request, 'django_apps/topic.html', context)
def new_topic(request):
"""Add a new topic."""
if request.method != 'POST':
# No data submitted; create a blank form.
form = TopicForm()
else:
# POST data submitted; process data.
form = TopicForm(data=request.POST)
if form.is_valid():
form.save()
return redirect('django_apps:topics')
# Display a blank name or invalid form.
context = {'form': form}
return render(request, 'django_apps/new_topic.html', context)
def new_entry(request, topic_id):
"""Add a new entry for a topic."""
topic_list = Topic.objects.get(id=topic_id)
if request.method != 'POST':
# NO data submitted; create a blank form.
form = EntryForm()
else:
# POST data submitted; process data.
form = EntryForm(data=request.POST)
if form.is_valid():
latest_entry = form.save(commit=False)
latest_entry.topic = topic_list
latest_entry.save()
return redirect('django_apps:topic', topic_id=topic_id)
# Display a blank name or invalid form.
context = {'topic_list': topic_list, 'form': form}
return render(request, 'django_apps/new_entry.html', context)
new_entry.html(updated!):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Study Tracker - Entry</title>
</head>
<body>
{% extends 'django_apps/base.html' %}
{% block content %}
{% for topic in topic_list %}
<p>{{ topic }}</p>
<p>Add a new entry:</p>
<form action="{% url 'django_apps:new_entry' topic_id=topic.id %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Add entry</button>
</form>
{% endfor %}
{% endblock content %}
</body>
</html>
base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Base template</title>
</head>
<body>
<p>
Study Tracker -
Topics
</p>
{% block content %}{% endblock content %}
</body>
</html>
forms.py:
from django import forms
from .models import Topic, Entry
class TopicForm(forms.ModelForm):
"""A class that defines the form in which a user can enter in a topic."""
class Meta:
"""This class tells django which model to base the form on and the
fields to include in the form."""
model = Topic
fields = ['text']
labels = {'text': ''}
class EntryForm(forms.ModelForm):
"""A class that defines the form in which a user can fill in an entry to
a topic."""
class Meta:
"""This meta class tells django which model to base the form for
entries on and the fields to include in the form."""
model = Entry
fields = ['text']
labels = {'text': 'Entry:'}
widgets = {'text': forms.Textarea(attrs={'cols': 80})}
I do not know how to go about fixing this issue. I've checked all my inheritance urls and templates for errors but I can't figure out what seems to the problem.
Update: Here is my topic.html template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Study Tracker - Topic</title>
</head>
<body>
{% extends 'django_apps/base.html' %}
{% block content %}
<p>Topic: {{ topic }}</p>
<p>Entries:</p>
<p>
Add new entry
</p>
<ul>
{% for entry in entries %}
<li>
<p>{{ entry.date_added|date:'M d, Y H:i' }}</p>
<p>{{ entry.text|linebreaks }}</p>
</li>
{% empty %}
<li>There are currently no entries for this topic.</li>
{% endfor %}
</ul>
{% endblock content %}
</body>
</html>
I also checked the topic and entry ids in shell too.
You passed your template topic_list variable but in your template you used topic. I think you would set a loop. Because you have no any variable named topic. If you changed them, it will work.
Can you post the template of your topics/<int:topic_id>/ view? (django_apps/topic.html)
To me, it looks like you have in this template a {% url 'new_entry' %} without specifying a topic.id. Because your path for this view is 'new_entry/<int:topic_id>/', the reverse url is failing and you see this error.
You probably want to change {% url 'new_entry' %} to {% url 'new_entry' topic.id %} or something like that.
You seem to be passing an empty variable to the template
# Display a blank name or invalid form.
context = {'topic': topic, 'form': form} # Change the context variable
return render(request, 'django_apps/new_entry.html', context)
new_entry.html
...
{% block content %}
<p>{{ topic }}</p>
<p>Add a new entry:</p>
<form
action="{% url 'django_apps:new_entry' topic_id=topic.id %}"
method="post"
>
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Add entry</button>
</form>
{% endblock content %}
...
Fixed it mate change your view.py file
add the parameter topic_id to your new_entry field
and a topic variable with Topic object
and topic in context
def new_entry(request, topic_id):
"""Add a new entry for a particular topic"""
topic = Topic.objects.get(id=topic_id)
if request.method != 'POST':
# No data submitted, create a blank form
form = EntryForm()
else:
# POST data submitted, Process Data.
form = EntryForm(data=request.POST)
if form.is_valid():
new_entry = form.save(commit=False)
new_entry.owner = request.user
new_entry.topic = topic
new_entry.save()
return redirect('logemys:topic', topic_id=topic_id)
# Display a blank or invalid form
context = {'topic': topic, 'form': form}
return render(request, 'logemys/new_entry.html', context)

Django search page

I'm trying to make a website that lets visitors search for books using another search engine. I have a script that takes a query, and returns some HTML with the results of the search, but I'm struggling to make a front end for this. I am using django because it seemed like the best option when I started, but now I am going in circles and I can't figure out how to make this thing - I'm just getting overwhelmed because the different tutorials and documentation that I'm reading all go into the advanced stuff before I can get the basic thing working.
Do I need separate search and results templates? Right now I'm getting the error The view book_search.views.search didn't return an HttpResponse object. It returned None instead.
How can I fix this error and/or design this whole thing better?
Here's what I have so far (the script that returns the results in html is pull.py):
The views and urls are from inside the book_search app.
views.py:
from django.shortcuts import render
from django.http import HttpResponse
from . import pull
from .forms import SearchForm
def index(request):
return HttpResponse("Welcome to the index page")
def test_search(request):
context = {'query': 'test query'}
return render(request, 'book_search/search.html', context)
def search(request):
if request.method == "GET":
form = SearchForm(request.GET)
if form.is_valid():
query = form.cleaned_data['query']
results = pull.main(query)
context = {'query': query, 'form': form, 'results': results}
return render(request, 'book_search/results.html', context)
apps.py:
from django.apps import AppConfig
class BookSearchConfig(AppConfig):
name = 'book_search'
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('index', views.index, name='index'),
path('test', views.test_search, name='test_search'),
path('', views.search, name='search'),
]
forms.py:
class SearchForm(forms.Form):
query = forms.CharField(label='Search', max_length=200)
template base.html:
<html>
<head>
</head>
<body>
<form method="GET" action="/search/">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% block content %}{% endblock %}
</body>
</html>
template results.html:
{% block content %}
{% results %}
{% endblock content %}
Since we guessed that form isn't valid (because no POST handler - you do not send anything to the form) and wrong indentation gives None response, now you can fix reference before assignment:
def search(request):
if request.method == "GET":
form = SearchForm()
context = {'form': form}
elif request.method == "POST":
form = SearchForm(request.POST)
if form.is_valid():
query = form.cleaned_data['query']
results = pull.main(query)
context = {'query': query, 'form': form, 'results': results}
return render(request, 'book_search/results.html', context)
and render errors in results.html template by putting this:
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}

NoReverseMatch at /

I'm getting a NoReverse match error. I've read several posts about this to find the answer, but I'm not seeing a solution.
This is a simple blog webapp for displaying posts in chronological order. The error is related to the edit_post function in "views.py." My suspicion is that the error has to do with trying to store the posts.id as an argument when modifying the post. I've tried removing the post.id in the offending line below and it will load the page. The problem is that if I do that, I cannot load the page for editing specific posts after that.
I don't understand what am I missing. I've looked at a number of posts dealing with this error, and I cannot identify the problem with my specific scenario. Any help is very much appreciated.
My error:
NoReverseMatch at /
Reverse for 'edit_posts' with arguments '('',)' and keyword arguments '{}' >not found. 1 pattern(s) tried: ['edit_posts(?P\d+)/']
Here is the offending line in the home page, "index.html":
<p>
edit post
</p>
Index view:
def index(request):
"""The home page for Blog."""
posts = BlogPost.objects.order_by('date_added')
context = {'posts': posts}
return render(request, 'blogs/index.html', context)
My "urls.py":
urlpatterns = [
# Home page
url(r'^$', views.index, name='index'),
# url(r'^posts/$', views.posts, name='posts'),
# Page for adding a new post.
url(r'^new_post/$', views.new_post, name='new_post'),
# Page for editing posts.
url(r'^edit_posts(?P<posts_id>\d+)/$', views.edit_posts,
name='edit_posts'),
]
edit_posts view:
def edit_posts(request, posts_id):
"""Edit an existing post."""
posts = BlogPost.objects.get(id=posts_id)
if request.method != 'POST':
# Initial request; pre-fill form with the current entry.
form = PostForm(instance=posts)
else:
# POST data submitted; process data.
form = PostForm(instance=posts, data=request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('blogs:index',
args=[posts.id]))
context = {'posts': posts, 'form': form}
return render(request, 'blogs/edit_posts.html', context)
Template for the "edit_posts.html" page:
{% extends "blogs/base.html" %}
{% block content %}
<p>Edit an existing post:</p>
<form action="{% url 'blogs:edit_posts' post.id %}" method='post'>
{% csrf_token %}
{{ form.as_p }}
<button name="submit">save changes</button>
</form>
{% endblock content %}
In your template, posts - as the name implies - is a queryset, ie a list of BlogPost objects. That queryset doesn't have an id attribute; only the individual posts within that list do.
If you want to link to a specific post, you need to loop through that list, and use the id of each post in the loop:
{% for post in posts %}
<p>
edit post
</p>
{% endfor %}

Categories