Prova12
Prova12

Reputation: 653

Django edit view does not show

I am new to Django.

My app allows a user to create a project by providing a title and a body content, delete it and update it.

Now, I am creating and edit/update view for my app but it is not showing up in my html page.

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 %}

PROBLEM

When I go to a urls such as http://localhost:8000/projects/2/editproject there is not the update form I want, the page exists but it contains nothing

Upvotes: 0

Views: 293

Answers (1)

Arjun Shahi
Arjun Shahi

Reputation: 7330

You have to pass the project = get_object_or_404(Project, pk=project_id) in the dictionary in order to get the value in template.

Change your view like this:

@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})

And in the template you can get value like this :

Also in your template change method=put to method=post and also you need to provide action if you want to update.

<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>

Upvotes: 1

Related Questions