Kevork
Kevork

Reputation: 307

Passing variable from django template to view

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.

Upvotes: 27

Views: 78206

Answers (3)

JJK
JJK

Reputation: 848

You can only send data to Django views from the template with 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.

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

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)

You would pass //localhost:8000/12/results/.

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

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.

# views.py
# Set the session variable
request.session['uid'] = 123456

# Retrieve the session variable
var = request.session.get('uid')

Upvotes: 48

Abhishek Poojary
Abhishek Poojary

Reputation: 809

To add to JJK's answer, here's the detailed way to pass a variable from template to view using form.

Template :

<form action="{% url 'INI:fbpages' SocialAccount.uid %}" method="post">
..
</form>

Above, 'INI:fbpages' is the url pattern name defined in urls.py

SocialAccount.uid is the variable that you want to pass to the view.

Note that the double curly braces used in variable rendering in django templates are not used here.

View :

This variable can be directly accessed in the view as the fbuser command argument

def fbpages(request, fbuser):
..

Upvotes: 3

Rajan Singh
Rajan Singh

Reputation: 19

We also use like this:

from django.urls import path
from . import views

urlpatterns = [
       path('articles/2003/', views.special_case_2003),
       path('articles/<int:year>/', views.year_archive),
       path('articles/<int:year>/<int:month>/', views.month_archive),
       path('articles/<int:year>/<int:month>/<slug:slug>/', views.article_detail),
]

Upvotes: 0

Related Questions