B
B
baterson2015-08-15 11:23:45
Django
baterson, 2015-08-15 11:23:45

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)

further form
class ArticleForm(ModelForm):
  class Meta:
    model = Article
    fields = ['article_text']

and then create a view
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})

Vue swears at the method and it seems that I am completely bogged down in understanding the logic. Please kick me in the right direction.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2015-08-15
@baterson

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 question

Ask a Question

731 491 924 answers to any question