从django模板传递变量来查看

从django模板传递变量来查看

本文介绍了从django模板传递变量来查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django 1.7构建我的第一个网站,并且很难弄清楚如何将变量从点击传递给视图。我的GET也是空的。



我的模板有一张带有Facebook帐户ID的表格,点击时应显示用户管理员的Facebook页面列表。



我的模板:

  {%for accountAccount 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%}

和我的观点:

  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 (请求,倡议/ ListFBPages.html,块)

如果我放URL中的ID,但我不想在URL中公开一个页面/用户标识。我觉得有一个简单的解决方案,但我还没有想到这一点。



感谢您的帮助。

解决方案

您只能从4种不同的方法从模板发送数据到Django视图。在您的情况下,如果您不想要URL中的信息,您可能只能使用选项1和4.

1。发布



所以你会提交一个有价值的表单。

 #你可以通过
request.POST.get('value')在你的views.py中检索你的代码

2。查询参数



所以你会传递// localhost:8000 /?id = 123

 #您可以通过
request.GET.get('id')


3。从URL(例如,请参阅)



所以你会传递// localhost:8000/12 / results /

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

视图...

 #views.py 
#检索(question_id)
def detail(请求,question_id):
...
返回HttpResponse(blahblah)

4。会话(通过cookie)



使用会话的缺点是您不得不将其传递给视图或更早地设置。



 #views.py 
#设置会话变量
request.session ['uid'] = 123456

#检索会话变量
var = request.session.get ['uid']


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.

解决方案

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.

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']

这篇关于从django模板传递变量来查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:55