Following this topic : Django's template doesn't render as expected (template language issue)
I’m continuing to train on a basic “Blogging app” where people can signup, login and publish articles.
Consequently I’ve created another app “app_articles”.
from django.db import models from app_accounts.models import User class Article(models.Model): author = models.ForeignKey(User,on_delete=models.CASCADE) title = models.CharField(max_length=20, unique=True, blank=False) message = models.CharField(max_length=20, blank=False) def __str__(self): return self.title
from django.shortcuts import render, redirect from .forms import publishArticleForm from app_accounts.models import User def publishArticle(request): if request.method == "POST": form = publishArticleForm(request.POST) if request.user.is_authenticated: # Issue is here, I can't really assign user id to "author" form["author"] = User.objects.get(pk=request.user.id) if form.is_valid(): form.save() else: print("ERROR : Form is invalid") print(form.errors) return redirect("app_base:index")
from django.forms import ModelForm from .models import Article class publishArticleForm(ModelForm): class Meta: model = Article fields = [ #Issue is here, because this field is required #But I want it to be linked to request.user.id "author", #The remaining fields will be shown through the form "title", "message", ]
Now the issue is that I get :
form["author"] = User.objects.get(pk=request.user.id) TypeError: 'publishArticleForm' object does not support item assignment
Do you have an idea on how to proceed ?
Thank you in advance