【问题标题】:Django url parameters in html codehtml代码中的Django url参数
【发布时间】:2018-08-01 13:42:29
【问题描述】:

我正在尝试将 todolist 项目的 id 传递给我的 html 代码,以便在我编辑 todolist 项目并单击提交时一切正常。

在views.py中:

def edit(request, id):
    todo = Todo.objects.get(id=id)
    context = {
        'todo': todo
    }

    if(request.method == 'POST'):
        title = request.POST['title']
        text = request.POST['text']

        todo.title = title


        todo.text = text

        todo.save()

        return redirect('/todos')

    else:
        return render(request, 'edit.html', context)

在 urls.py 中:

url(r'^details/(?P<id>\w{0,50})/edit/$', views.edit, name='edit')

在 HTML 中:

<h2>Edit Todo- {{ todo.title }}</h2>

<form action="{% url 'edit' request.todo.id %}" method="post">
{% csrf_token %}
<label for="title">Title</label> <br/>

<input type="text" name="title" id="title"/>
<br>

<label for="text">Text</label> <br/>
<textarea type="text" name="text" id="text"></textarea>
<br><br>

<input type="submit" value="Submit"/>
</form>

我认为问题出在我的 html 代码中的 url。我不知道如何传递我的 todolist 项的 id 参数。

我不断收到的错误是

invalid literal for int() with base 10: ''

请帮忙

【问题讨论】:

  • 只需 {% url 'edit' todo.id %} 在表单操作中,或将其留空

标签: python html django pycharm


【解决方案1】:

问题出在:,不要使用“request”,通过上下文发送的对象是“todo”:

<form action="{% url 'edit' todo.id %}" method="post">{% csrf_token %}

或者您可以将其留空。空白表示当前显示的视图

<form action="" method="post">{% csrf_token %}

【讨论】:

    猜你喜欢
    • 2021-10-27
    • 2012-08-03
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多