How to send parameter to view?

20 views
Skip to first unread message

Kubilay Yazoğlu

unread,
Dec 18, 2017, 10:36:43 AM12/18/17
to Django users
Hello. I have two apps. Posts and Contests.

In templates/contest/detail.html, there is a button that takes you to templates/post/create.html

What I want is to send the contest name from detail to create so that the post will be added to the right contest. For that, I guess I should do something to send contest name from detail.html to create view of post/create. Right?
In the following code (post\views.py), the contest name is determined randomly. Help me to get the right contest please.

if request.method == 'POST':
items = Contest.objects.all()
random_item = random.choice(items)
form = PostForm(request.POST, request.FILES)

if form.is_valid():
post = form.save(commit = False)
post.contest = random_item
post.ownername = request.user
post.save()
messages.success(request, 'You have succesfully sent a post')
return HttpResponseRedirect(random_item.get_absolute_url())
else:
form = PostForm()

Etienne Robillard

unread,
Dec 19, 2017, 6:01:36 AM12/19/17
to Kubilay Yazoğlu, django...@googlegroups.com

Hi Kubilay,

I guess you could use sessions for that.

For example:

contest = request.session['contest_name']

See: https://docs.djangoproject.com/en/2.0/topics/http/sessions/

HTH

Etienne

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ddc3b1d3-32a3-4457-a36d-5cbbea6255b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Etienne Robillard
tka...@yandex.com
https://www.isotopesoftware.ca/
Reply all
Reply to author
Forward
0 new messages