python - 如何从 url 中的模板传递 pk 以在 View "Django"中使用?

标签 python django html

我收到此错误: “值错误位于/5 以 10 为基数的 int() 的文字无效:'' "

我认为我正在传递一个字符串并尝试将其用作 int。如何正确传递我想要的int?

这是我的代码, 在 html 模板中:

    <form method="GET" action="{% url 'register' activity.pk %}">
             {% csrf_token %}
            <button 
            type="submit" 
            class="btn btn-primary" 
            id="Register">Register
            </button>
    </form>

在 urls.py 中:

 url(r'^(?P<pk>)', authentication_views.registerAttende, name='register'),

在views.py中:

def registerAttende(request,pk):
    act = Activity.objects.get(pk=pk) //This is the line that is causing the error
    act.save()
    attend = act.attendee.create(student=request.user.id)
    return render(request, 'dashboard.html')

我得到变量 pk 的值为:u'' /image/uSw24.png

我该如何解决这个问题?我做错了什么?

最佳答案

您的正则表达式是错误的 - 您没有提供要捕获的模式。应该是:

r'^(?P<pk>\d+)'

关于python - 如何从 url 中的模板传递 pk 以在 View "Django"中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47397800/

相关文章:

python - VS Code 抛出错误 "Error while fetching extensions : XHR failed"

python - 如何在 Keras CNN 模型中预测单个图像?

python - 两个方括号并排 : strange effect

python - 在 Django View 中高效地导入模块

javascript - 仅当文本超过 x 行时才显示文本

java - 在 O(n) 时间内找到正确的路径

python - 密码重置功能 Django-Registration-Redux 应用程序

python - django-paypal 无法处理通知信号

html - 优化图像

html - 在此底部代码中添加链接代码