Closed alanjameschapman closed 8 months ago
Basic crispy form displayed within new create_post template | Slug, status and RTF can only be edited within admin panel |
---|---|
Validation to ensure title is unique
Amend view:
if form.is_valid():
post = form.save(commit=False) # Don't save the form to the database yet
post.author = request.user # Set the author field to the current user
post.slug = slugify(post.title)
try:
post.save() # Try to save the form to the database
except IntegrityError as e: # If the slug is not unique, add an error to the form
if 'slug' in str(e):
form.add_error('title', 'A post with this title already exists - please choose a different title. Case, punctuation and spacing are ignored.')
return render(request, 'edblog/create_post.html', {'form': form})
raise e
return redirect('home')
Amend model:
title = models.CharField(
max_length=200,
unique=True,
error_messages={
'unique': 'A post with this title already exists - please choose a different title. Case, punctuation and spacing are ignored.',
},)
User given specific feedback:
Summernote widget added to forms.py
from django_summernote.widgets import SummernoteWidget
...
class PostForm(forms.ModelForm):
...
content = forms.CharField(widget=SummernoteWidget())
Add styled cancel button (anchor tag linked to home) and style submit button:
<button type="submit" class="btn btn-success" aria-label="Submit">Submit</button>
<a href="{% url 'home' %}" class="btn btn-danger" aria-label="Cancel">Cancel</a>
Screenshot:
Modify the create_post view to set the status of the post to 0 for "Draft" and 1 for "Published" when the respective button is clicked:
def create_post(request):
...
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False) # Don't save the form to the database yet
post.author = request.user # Set the author field to the current user
post.slug = slugify(post.title)
if 'submit' in request.POST:
post.status = 1 # publish the post
elif 'save_draft' in request.POST:
post.status = 0 # save the post as a draft
try:
post.save() # Try to save the form to the database
https://github.com/alanjameschapman/whiteboard/assets/137620143/025819b9-b4fb-494c-b5d9-702e6dcc5053
As a teacher I can post content so that I can provide information to students on a topic of my choice.
Acceptance Criteria
Tasks