所以我创建了一个django网站,我有一个编辑按钮。 编辑显示弹出窗口,其中包含一些要编辑的表单。问题是要编辑的表单是空白的!而不是拥有以前的数据。例如- 服务器名="贝纳"当我点击编辑而不是" Yossi"我没有任何形式。
我需要将哪些内容添加到index.html或PostEdit类中,以便我在表单中包含以前的数据,而不是空白表单?
models.py -
from django.db import models
# Create your models here.
class serverlist(models.Model):
ServerName = models.CharField(max_length = 30)
Owner = models.CharField(max_length = 50)
Project = models.CharField(max_length = 30)
Description = models.CharField(max_length = 255)
IP = models.CharField(max_length = 30)
ILO = models.CharField(max_length = 30)
Rack = models.CharField(max_length = 30)
Status = models.CharField(max_length = 30)
#date = models.DateTimeField(auto_now=True)
views.py -
# Create your views here.
from django.shortcuts import render_to_response
from django.shortcuts import render, redirect
from django.template import RequestContext
from django.views.generic import TemplateView, UpdateView, DeleteView, CreateView
from DevOpsWeb.forms import HomeForm
from DevOpsWeb.models import serverlist
from django.core.urlresolvers import reverse_lazy
from simple_search import search_filter
from django.db.models import Q
class HomeView(TemplateView):
template_name = 'serverlist.html'
def get(self, request):
form = HomeForm()
query = request.GET.get("q")
posts = serverlist.objects.all()
if query:
posts = serverlist.objects.filter(Q(ServerName__icontains=query) | Q(Owner__icontains=query) | Q(Project__icontains=query) | Q(Description__icontains=query) | Q(IP__icontains=query) | Q(ILO__icontains=query) | Q(Rack__icontains=query))
else:
posts = serverlist.objects.all()
args = {'form' : form, 'posts' : posts}
return render(request, self.template_name, args)
def post(self,request):
form = HomeForm(request.POST)
posts = serverlist.objects.all()
if form.is_valid(): # Checks if validation of the forms passed
post = form.save(commit=False)
post.save()
text = form.cleaned_data['ServerName']
form = HomeForm()
return redirect('serverlist')
args = {'form': form, 'text': text}
return render(request, self.template_name,args)
class PostDelete(DeleteView):
model = serverlist
success_url = reverse_lazy('serverlist')
class PostEdit(UpdateView):
model = serverlist
fields = ['ServerName','Owner','Project','Description','IP','ILO','Rack','Status']
success_url=reverse_lazy('serverlist')
urls.py -
from django.conf.urls import url, include
from DevOpsWeb.views import HomeView
from DevOpsWeb.views import PostDelete
from DevOpsWeb.views import PostEdit
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
# Examples:
url(r'^$', HomeView.as_view(), name='serverlist'),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
#DevOpsWeb:8000/Delete/
url(r'^delete/(?P<pk>\d+)/$', PostDelete.as_view(), name="delete_post"),
url(r'^django_popup_view_field/', include('django_popup_view_field.urls', namespace="django_popup_view_field")),
url(r'^admin/', include(admin.site.urls)),
#DevOpsWeb:8000/edit/
url(r'^edit/(?P<pk>\d+)/$', PostEdit.as_view(), name="edit_post"),
serverlist.html -
<button type="button" class="btn btn-outline-primary" data-toggle="modal" href="#edit-server-{{server.id}}"
data-target="#Edit{{server.id}}">   Edit  <i class="fa fa-pencil"></i></button>
 
<div id ="Del{{server.id}}" class="modal fade" role="document">
<div class="modal-dialog" id="delete-server-{{server.id}}">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete Confirmation</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="{% url 'delete_post' server.id %}" method="post">{% csrf_token %}
<h6>Are you sure you want to delete {{ server.ServerName }}?</h6>
<br>
<center><input type="submit" class="btn btn-danger btn-md" value="Confirm"/>
<button type="submit" class="btn btn-secondary" data-dismiss="modal">Cancel</button></center>
</form>
</div>
</div>
</div>
</div>
<div class="modal fade bd-example-modal-sm" id="Edit{{server.id}}" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edit Server</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="{% url 'edit_post' server.id %}" method="post"> {% csrf_token %}
<!--<center> {{ form.as_p }} </center> -->
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
<!-- {{ field.label_tag }} -->
<small><b>{{ field.html_name }}<p align="left"></b> {{ field }}</small> </p>
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
</div>
{% endfor %}
<center><h2> <button type="submit" class="save btn btn-success btn-lg">Confirm</button></h2>
<h2><button type="submit" class="btn btn-secondary" data-dismiss="modal">Cancel</button></h2></center>
</form>
</td>
</div>
</tr>
{% endfor %}