Answer the question
In order to leave comments, you need to log in
Am I completely confused by the django logic?
Hey guys, I'm having a problem with django. I went through the tutorial on the djangobook, looked at the tutorials on YouTube, still I still can’t solve simple problems.
Added registration, now wanted to add articles from users and further adding files
Are my steps going in the right direction?
Create article model:
class Article(models.Model):
class Meta():
db_table = 'article'
article_text = models.TextField()
article_user = models.ForeignKey(User)
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['article_text']
def cabinet(request):
user = User.objects.all()
if request.method == 'POST':
form = ArticleForm(request.Post)
if form.is_valid():
user.article_set.create(article_text=form)
return redirect('/')
else:
form = ArticleForm()
return render(request, 'cabinet.html', {'form': form})
Answer the question
In order to leave comments, you need to log in
stackoverflow.com/questions/17126983/add-data-to-m...
def cabinet(request):
# user = User.objects.all() зачем брать всех пользователей?
form = ArticleForm(request.POST or None)
if request.method == 'POST' and form.is_valid():
obj = form.save(commit=False)
obj.article_user = request.user
obj.save()
return redirect('/')
return render(request, 'cabinet.html', {'form': form})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question