When posting a new post on the posting site, instead of typing in your own name, you want to set the logged-in user's information (such as your name) in the form in advance and save the trouble of typing in your name.
app/models.py
from django.db import models
from django.contrib.auth import get_user_model
class Post(models.Model):
author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
title = models.CharField(max_length=100)
content = models.TextField()
def __str__(self):
return self.title
Prepare a Post model and set up three fields [Poster, Title, Content].
ʻAuthor = models.ForeignKey (get_user_model (), on_delete = models.CASCADE)get_user_model ()
calls the entire class of the currently logged-in user, and this one line calls the ʻauthor` field and the logged-in user. Is linked.
There are several ways to refer to the user model, but it is not good to refer to the user model directly.
Reference: https://djangobrothers.com/blogs/referencing_the_user_model/
app/forms.py
from django import forms
from .models import Post
class PostForm(forms.ModelForm):
class Meta():
model = Post
fields = (
'title',
'content',
)
This time, I used a model form. Create a PostForm class by inheriting the ModelForm included in forms.py provided by Django.
Specify the model to be inherited by model = Post
, and specify two fields except author. There is no need to bother to specify the author here.
app/views.py
from django.shortcuts import render, redirect
from .models import Post
from . import forms
def form_view(request):
if request.method == 'POST':
form = forms.PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
return redirect('sns:index')
else:
form = forms.PostForm()
return render(request, 'sns/form_view.html', {'form': form})
In the form_view function, it is necessary to change the behavior depending on whether POST is received as request (ʻif request.method =='POST': ) or GET (ʻelse:
). For example, when the button "New Post" is pressed and the form_view function is called by the URL dispatcher, the request becomes'GET', so the process of simply displaying the form is executed as shown below.
else:
form = forms.PostForm()
return render(request, 'app/form_view.html', {'form': form})
When something is entered in the displayed form, the submit button is pressed, and the form_view function is called by the URL dispatcher, the request becomes'POST', so the code shown below is executed.
if request.method == 'POST':
form = forms.PostForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
return redirect('app:index')
form = forms.PostForm (request.POST)
in form.is correct, and save
formwith
post = form.save (commit = False). At this time, set
commit = False` in the argument and tell that it will be modified a little more before saving completely.post.author = request.user
, specify the request
ʻuser` (that is, the logged-in user) in the author field of post.post.save ()
and the logged-in user name.return redirect ('app: index')
.Recommended Posts