Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable from django template to view

Tags:

I'm building my first site with django 1.7 and am having a hard time figuring out how to pass a variable from a click to a view. My GET is also empty.

My template has a table with Facebook Account IDs, when clicked should show a list of Facebook pages that user Admins.

My template:

{% for SocialAccount in accountlist %}    <tr>       <td><a href="{% url 'INI:fbpages' %}">{{ SocialAccount.uid }}</a></td>       <td>{{ SocialAccount.extra_data.first_name }}</td>       <td>{{ SocialAccount.extra_data.last_name }}</td>       <td>{{ SocialAccount.extra_data.email }}</td>    </tr> {% endfor %} 

and my view:

def fbpages(request, fbuser):     djuser = request.user.id     context = RequestContext(request)     fbuser = 1234634     pagelist = facebook.pages(request, djuser, fbuser)     blocks = {'title': 'Facebook Pages',           'pagelist': pagelist}     return render(request, "initiative/ListFBPages.html", blocks) 

I could do this easily if I put the ID in the URL but I don't want to expose a page/user ID in the url. I feel like there's an easy solution but I haven't figured it out.

Thanks for you help.

like image 946
Kevork Avatar asked Mar 19 '15 19:03

Kevork


People also ask

What is a more efficient way to pass variables from template to view in Django?

POST form (your current approach)

How can you display all variables in the page on to a Django template?

There's an option called 'Templates' with another option to 'Toggle context' and you can see all the variables passed to your template, as well as the ability to see the code behind the template.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.


1 Answers

You can only send data to Django views from the template in 4 different methods. In your case you will probably only be able to use option 1 and 4 if you don't want the information in the URL.

Since I am new to StackOverflow, I can't post more than 2 links so if you search the following post you will find more information about the advantages and disadvantages of each method.

"what is a more efficient way to pass variables from template to view in django"

1. Post

So you would submit a form with value.

    # You can retrieve your code in your views.py via     request.POST.get('value') 

2. Query Parameters

So you would pass //localhost:8000/?id=123

    # You can retrieve your code in your views.py via     request.GET.get('id') 

3. From the URL (See here for example)

So you would pass //localhost:8000/12/results/

    # urls.py     urlpatterns = patterns(         ...         url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),         ...     ) 

and in your views...

    # views.py     # To retrieve (question_id)     def detail(request, question_id):         ...         return HttpResponse("blahblah") 

4. Session (via cookie)

Downside of using session is you would have had to pass it to the view or set it earlier.

https://docs.djangoproject.com/en/1.7/topics/http/sessions/

    # views.py     # Set the session variable     request.session['uid'] = 123456      # Retrieve the session variable     var = request.session.get['uid'] 
like image 123
JJK Avatar answered Sep 21 '22 11:09

JJK