Django编辑视图未显示

时间:2019-07-07 10:34:50

标签: django

我是Django的新手。

我的应用允许用户通过提供标题和正文内容来创建project,然后将其删除并更新。

现在,我正在为我的应用创建和编辑/更新视图,但是它没有显示在我的html页面中。

urls.py

from django.urls import path, include
from . import views

urlpatterns = [
    path('allprojects', views.allprojects, name='allprojects'),
    path('createproject', views.createproject, name='createproject'),
    path('<int:project_id>', views.projectdetail, name='projectdetail'),
    path('<int:project_id>/editproject', views.editproject, name='editproject'),

]

projects / views.py

@login_required
def editproject(request, project_id):
  if request.method == 'POST':
    if request.POST['title'] and request.POST['content']:
      project = get_object_or_404(Project, pk=project_id)
      project.title = request.POST['title']
      project.content = request.POST['content']
      project.developer = request.user
      project.save()
      return redirect('/projects/' + str(project.id))
    else:
      return render(request, 'projects/editproject.html', {'error':'All fields are required.'})
  else:
    return render(request, 'projects/allprojects.html')

projects / templates / projects / editproject.html

{% extends 'base.html' %}
{% block title %}Edit Project{% endblock %}
{% block content %}


<div class="container">
  <div class="row">
    <div class="mt-4 offset-md-3 col-md-6">
      <h2>Create a new project</h2>
      <form method="post">
        {% csrf_token %}
        <div class="form-group">
          <label for="exampleFormControlTextarea1">Title of the project</label>
          <textarea class="form-control" id="exampleFormControlTextarea1" rows="1" name="title"></textarea>
        </div>
        <div class="form-group">
          <label for="exampleFormControlTextarea1">Brief description of this project</label>
          <textarea class="form-control" id="exampleFormControlTextarea1" rows="5" name="content"></textarea>
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
      </form>
    </div>
  </div>
</div>


{% endblock %}

问题

当我转到http://localhost:8000/projects/2/editproject之类的网址时,没有我想要的更新表单,该页面存在,但其中不包含任何内容

1 个答案:

答案 0 :(得分:1)

您必须在字典中传递project = get_object_or_404(Project, pk=project_id)才能在模板中获取值。

这样更改您的视图:

@login_required
def editproject(request, project_id):
  project = get_object_or_404(Project, pk=project_id)
  if request.method == 'POST':
    if request.POST['title'] and request.POST['content']:
      project.title = request.POST['title']
      project.content = request.POST['content']
      project.developer = request.user
      project.save()
      return redirect('/projects/',project.project_id)
  return render(request, 'projects/editproject.html', {'error':'All fields are required.','project':project})

template中,您可以像这样获得价值:

还要在模板中将method=put更改为method=post 并且如果要更新,还需要提供action

<div class="form-group">
<label for="exampleFormControlTextarea1">Title of the project</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="1" name="title">
{{project.title}}
</textarea>
 </div>