Django UpdateView有空表单

时间:2017-11-27 15:51:43

标签: python html mysql django database

我正在使用UpdateView使用表单编辑数据。

在点击编辑按钮后,会弹出一个模式,其中包含一些可以编辑的表格,然后在我编辑后点击确认我重定向到/编辑/ {PK}。

问题是表格是空白的!是全新的..我希望表格中的先前数据已经不是空白了。

view.py -

from django.shortcuts import render_to_response
from django.shortcuts import get_object_or_404
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)
            #if not form.cleaned_data['ServerName']:
            #post.servername = " "
            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
    #post = serverlist.objetcs.get(server_id=server_id)
    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"),
]

forms.py -

from django import forms
from DevOpsWeb.models import serverlist


class HomeForm(forms.ModelForm):
    ServerName = forms.CharField(widget=forms.TextInput,max_length = 30,required=False)
    Owner = forms.CharField(max_length = 50,required=False)
    Project = forms.CharField(max_length = 30,required=False)
    Description = forms.CharField(max_length = 255,required=False)
    IP = forms.CharField(max_length = 30,required=False)
    ILO = forms.CharField(max_length = 30,required=False)
    Rack = forms.CharField(max_length = 30,required=False)
    Status = forms.CharField(max_length = 30,required=False)
    class Meta:
        model = serverlist
        fields = ('ServerName' ,'Owner','Project','Description','IP','ILO','Rack','Status',)

class AutoCompleteModelChoiceField(forms.ModelChoiceField):
    widget = forms.TextInput
    def clean(self, value):
        value = super(AutoCompleteModelChoiceField, self).clean(value)
        return value


class serverForm(forms.ModelForm):
    hotel = AutoCompleteModelChoiceField(queryset=serverlist.objects.all())

index.html(带编辑按钮的部分) -

            <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 <b>{{ server.ServerName }}</b> </h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</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 %}
                </div>
                <div class="wrapper">
                &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                <h2><button type="submit" class="save btn btn-success btn-lg">Confirm</button></h2>&nbsp&nbsp&nbsp
                                <h2><button type="submit" class="btn btn-secondary btn-lg" data-dismiss="modal">Cancel</button></h2>
                </div>
                </form>
            </td>
                </div>
        </tr>
                {% endfor %}

1 个答案:

答案 0 :(得分:1)

您的代码的问题是您的success_url调用HomeView,因此调用HomeView的get()方法。在get()方法中,您再次创建了一个新的表单对象。您必须使用要更新的实例初始化表单:

form = HomeForm(instance = your_serverlist_object)

因为您必须在第一次和编辑后调用相同的HomeView,您可以传递一些查询参数来检测您需要在上下文中发送哪种表单对象。类似: success_url="%s?edit=true" % reverse_lazy('serverlist')

现在在HomeView覆盖调度方法。

class HomeView(TemplateView):

    template_name = 'serverlist.html'
    def dispatch(self, request, *args, **kwargs)
        query = request.GET.get("q")

        if query:
            self.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:
            self.posts = serverlist.objects.all()

    def get(self, request):
        if request.GET.get('edit', ''):
            form = HomeForm(instance = self.posts)
        else:
            form = HomeForm()
        args = {'form' : form, 'posts' : self.posts}
        return render(request, self.template_name, args)