简体   繁体   English

Django-从模板传递参数到视图不起作用

[英]Django - Passing parameters from template to view not working

I'm trying to get my hands dirty with django and I started trying to make my own project. 我试图弄清django的手,然后开始尝试制作自己的项目。 I'm currently having trouble passing parameters from my template to my views without using POST requests. 我目前无法在不使用POST请求的情况下将参数从模板传递到视图。

heres my code in the template 我的代码在模板中

#in main.html

<div>
    {{ event_results }}
    {{ friends }}
</div>

{% for user in results %}
    <div class="userblock">
        <p class="user">{{ user.username }}</p>
            <a href="/events/addFriend/{{user.username}}">
                <button class="navbuttons" id="addfriend">Add as friend</button>
            <a/>
    </div>
{% endfor %}

#in urls.py

from django.conf.urls import patterns, url
from events import views, eventview

url(r'^addFriend/(<requested_friend>[a-z]*)/', views.addFriend, name='addFriend'),
)

#in views.py

def addFriend(request, requested_friend):
    currentUser = User.objects.get(username=request.session['username'])
    try:
        list_of_friends = Friends.objects.get(username=currentUser)
    except (KeyError, Friends.DoesNotExist):
        return render(request, 'events/main.html', {'friends': requested_friend})
    else:
        return render(request, 'events/main.html', {'friends':list_of_friends})

So when I click on the button "Add friend" in main.html, it goes to the url.py and maps it to the function addFriend in views.py with the argument and from there it does its magic. 因此,当我单击main.html中的“添加朋友”按钮时,它将转到url.py并将其映射到views.py中带有参数的addFriend函数,并从那里开始发挥作用。 However, it's not taking in the argument. 但是,它并没有考虑参数。 I know I'm doing something wrong in the urls.py with the regex but im not sure what. 我知道我用正则表达式在urls.py中做错了,但我不确定是什么。 Any advice is greatly appreciated. 任何意见是极大的赞赏。 Thanks! 谢谢!

When you change (<requested_friend>[az]*) to (?P<requested_friend>[0-9A-Za-z_\\-]+) than everything looks fine. 当您将(<requested_friend>[az]*)更改为(?P<requested_friend>[0-9A-Za-z_\\-]+) ,一切看起来都很好。

But remember to use + instead of * in the pattern. 但是请记住在模式中使用+而不是* * matches also a empty string ( addFriend// is matched) but with + the string must have at least one character ( addFriend// isn't matched) *也匹配一个空字符串(匹配addFriend// ),但使用+字符串必须至少包含一个字符(不匹配addFriend//

You can add $ on the end of url pattern r'^addFriend/(?P<requested_friend>[0-9A-Za-z_\\-]+)/$' Here you can find why. 您可以在网址格式r'^addFriend/(?P<requested_friend>[0-9A-Za-z_\\-]+)/$'的末尾添加$ 。在这里,您可以找到原因。

Also check if link in browser has correct value /events/addFriend/<user_name>/ maybe is something wrong with {{ user.username }} 另外,请检查浏览器中的链接的值是否正确/events/addFriend/<user_name>/可能与{{ user.username }}

You have error in urls.py. 您在urls.py中出错。 In named group pattern you miss ?P prefix. 在命名组模式中,您会错过?P前缀。 See doc for reference . 请参阅doc以获取参考

Instead of 代替

url(r'^addFriend/(<requested_friend>[a-z]*)/', views.addFriend, name='addFriend'),

It should be: 它应该是:

url(r'^addFriend/(?P<requested_friend>[a-z]*)/', views.addFriend, name='addFriend'),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM