Passing a variable from a django template for viewing

I am building my first website with django 1.7, and it’s hard for me to figure out how to pass a variable from one click to a view. My get is also empty.

My template has a table with Facebook account IDs, when I click on it, a list of Facebook pages that are the Admins user should be displayed.

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 opinion:

 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 make it easy if I put the id in the url, but I don't want to show the page / user id in the url. I feel that there is a simple solution, but I did not understand this.

Thanks for the help.

+6
source share
1 answer

You can send data only in Django views from a template in 4 different ways. In your case, you can probably only use options 1 and 4 if you do not need the information in the URL.

Since I'm new to StackOverflow, I cannot post more than two links, so if you find the following message, you will find more information about the advantages and disadvantages of each method.

"which is a more efficient way to pass variables from a template for viewing in django"

1. Message

So you must submit a form with a value.

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

2. Request parameters

So, you 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 )

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)

The disadvantage of using a session is that you would have to submit it for viewing or set it up 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'] 
+17
source

Source: https://habr.com/ru/post/984019/


All Articles