简体   繁体   English

无法更新CharField-Django

[英]Unable to update CharField - Django

First of all I'm glad to be here, I read you lately and i found useful answers here. 首先,我很高兴来到这里,最近我读了你的书,在这里找到了有用的答案。
This is my first post so please be kind with me, I'm a newbie in programming. 这是我的第一篇文章,所以请对我好一点,我是编程方面的新手。

So, I'm writing my 1st web application in Django - a todo app and I don't know how to write the function that does this this. 因此,我正在用Django编写我的第一个Web应用程序-一个todo应用程序,我不知道如何编写执行此操作的函数。 I found something in Django docs and in other related discussions but it doesn't work. 我在Django文档和其他相关讨论中找到了一些东西,但这是行不通的。

Here's my code: 这是我的代码:

#models.py

class Task(models.Model):
user = models.ForeignKey(User)
task = models.CharField(max_length=200)
initialized_at = models.DateTimeField(auto_now_add=True)
due_date = models.DateField(default=datetime.now)
done = models.BooleanField(default=False)

def __unicode__(self):
    return self.task

#views.py
def edit_task(request, id):
if request.method == 'POST':
    task_to_edit = Task.objects.get(pk=task_id)
    form = TaskForm(request.POST, instance=task_to_edit)
    form.save()
    if form.is_valid():
        task_to_edit = form.save()
    return HttpResponseRedirect('/')
else:
    form = TaskForm()
return render(request, 'todo/edit_task.html', {'form': form})

#urls.py
url(r'^edit_task/(?P<task_id>\w+)/$', 'todo.views.edit_task')

#edit_task.html
{% block content %}
<form action="/edit_task/" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>
{% endblock content %}

When I submit the updated form I get this error: 当我提交更新的表格时,出现此错误:

Page not found (404)
Request Method:     POST
Request URL:    hxxp://127.0.0.1:8000/edit_task/

Using the URLconf defined in jbz.urls, Django tried these URL patterns, in this order: Django使用jbz.urls中定义的URLconf,按以下顺序尝试了以下URL模式:

^admin/
^$ [name='index']
^(?P<task_id>\d+)/$
^(?P<task_id>\d+)/$
^add-task/$
^delete-task/(?P<task_id>\w+)/$
^edit_task/(?P<id>\w+)/$
^done/(?P<task_id>\d*)/$

The current URL, edit_task/ , didn't match any of these. 当前网址edit_task/与其中任何一个都不匹配。

and the root urls.py looks like: 根urls.py看起来像:

url(r'', include('todo.urls'))
#edit_task.html
{% block content %}
<form action="/edit_task/{{task.id}}" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>
{% endblock content %}

Notice how I added {{task.id}} expression in <form action="/edit_task/{{task.id}}" method="post"> 请注意,我是如何在<form action="/edit_task/{{task.id}}" method="post">添加{{task.id}}表达式的

IMPORTANT NOTE: Substitute {{task.id}} to whatever variable accomplishes this in your template. 重要说明:{{task.id}}替换为在模板中完成此操作的任何变量。

The reason why you get the error is because edit_task/ is not getting the other part, task_id to match the regular expression: 出现错误的原因是因为edit_task/没有得到另一部分task_id以匹配正则表达式:

url(r'^edit_task/(?P<task_id>\w+)/$', 'todo.views.edit_task')

UPDATE: Also your edit_task view has potential errors as well> 更新:同样,您的edit_task视图也有潜在的错误>

def edit_task(request, id):
    task_to_edit = Task.objects.get(pk=id)
    if request.method == 'POST':
        form = TaskForm(request.POST, instance=task_to_edit)
        form.save()
        if form.is_valid():
            task_to_edit = form.save()
        return HttpResponseRedirect('/')
else:
    form = TaskForm(instance=task_to_edit)
# you don't pass any task variable to the view so the form view
# won't know which task to edit, you'll have to handle that
return render(request, 'todo/edit_task.html', {'form': form, 'task':task_to_edit})

Note: I corrected the code in the view a little. 注意:我稍微纠正了视图中的代码。 Now the task_to_edit is passed also to the Form to fill the fields when the view is requested via GET . 现在,当通过GET请求视图时, task_to_edit也将传递到表单以填充字段。 Notice that in order to access to this view, the url in the browser should look like this http://www.example.com/edit_task/2 请注意,为了访问此视图,浏览器中的网址应如下所示http://www.example.com/edit_task/2

If other wise you try to access http://www.example.com/edit_task without passing the id you'll get Error 404 . 否则,如果您尝试访问http://www.example.com/edit_task而不传递ID,则会收到错误404

Hope this helps! 希望这可以帮助!

I think your pattern for edit task expects an id - task name. 我认为您的编辑任务模式需要一个ID-任务名称。 Try changing your URL pattern: 尝试更改您的网址格式:

'^edit_task/(?P<task_id>\w+)/$'

to

'^edit_task/$'

or providing the task id that you want to edit. 或提供您要编辑的任务ID。

Just add name space to your url and according update your template. 只需在您的网址中添加名称空间,然后根据您的模板进行更新即可。

#urls.py
url(r'^edit_task/(?P<task_id>\w+)/$', 'todo.views.edit_task', name= "edit_task")

#edit_task.html
{% block content %}
<form action="{% url 'edit_task' task_id %}" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>
{% endblock content %}

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

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