Django中的UpdateView显示空白表单而不是数据库以前的数据

Posted

技术标签:

【中文标题】Django中的UpdateView显示空白表单而不是数据库以前的数据【英文标题】:UpdateView in Django shows blank forms instead of Database previous data 【发布时间】:2018-05-11 15:59:27 【问题描述】:

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

在我点击编辑popup using modal 后,显示的表单带有空白数据!它不会检索数据库中以前的数据。 有人知道我应该添加什么吗?

我被这个编辑卡住了大约一个星期:(

如果有人有线索,我将不胜感激! 谢谢!

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="Editserver.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 %

帖子和编辑按钮:

                % for server in posts %

        <tr>
          <div class ="server">
            <td></td>
            <td><center> server.ServerName </center></td>
            <td><center> server.Owner </center></td>
            <td><center> server.Project </center></td>
            <td><center> server.Description </center></td>
            <td><center> server.IP </center></td>
            <td><center> server.ILO </center></td>
            <td><center> server.Rack </center></td>
            <td><h4><span class="badge badge-success"> server.Status </span></h4></td></center>

            <td>

&nbsp&nbsp&nbsp&nbsp
            <button type="button" class="btn btn-outline-danger" data-toggle="modal" href="#delete-server-server.id"
             data-target="#Delserver.id">Delete <i class="fa fa-trash-o"></i></button>&nbsp
            <button type="button" class="btn btn-outline-primary" data-toggle="modal" href="#edit-server-server.id"
             data-target="#Editserver.id"> &nbsp&nbspEdit&nbsp           <i class="fa fa-pencil"></i></button>
&nbsp


                        <div id ="Delserver.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">&times;</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="Editserver.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 %
    </tbody>
</h5>
    </table>

【问题讨论】:

不太了解你的views.pyHomeView 的班级在哪里?您的 get()post() 函数不在类中... 您是否真的要在您的一般HomeView 中嵌入多个PostEdit 视图? 函数get和post在HomeView类下。我不在第二个问题下:( 【参考方案1】:

您没有使用要更新的实例来初始化表单:

form = HomeForm()

而不是当您希望使用来自对象的数据填充表单时:

form = HomeForm(instance=my_object_to_update)

由于您想为显示在HomeView 中的每个post 执行此操作,因此您需要为每个post 初始化不同的表单。在HomeView.get()HomeView.post() 方法中,除了创建用于创建新serverlist 的空HomeForm()

for post in posts:
    forms[post.id] = HomeForm(instance=post)

在你的模板渲染中(假设你的循环% for server in posts %

% with server.id as id %
    % with forms|get_by_key:id as edit_form %
        <form action="% url 'edit_post' server.id %" method="post">
           ... display the form: your current code % for field in edit_form % etc...
        </form> 
    % endwith %
% endwith %

我正在使用自定义模板过滤器|get_by_key:variable在使用变量时按键获取dict值:

@register.filter(name='get_by_key')
def get_by_key(dictionnary, key):
"""Return element in dictionnary corresponding to `key`."""
    return dictionnary[key] if isinstance(dictionnary, dict) else ''

由于您有一个模板来创建新的服务器列表,而且还可以显示所有现有的服务器列表对象并更新它们,因此您需要两种表单。因此,创建上面的forms dict 是除了之外还有创建表单HomeForm()。你的上下文应该有:

args = 
    'form': form,  # this is the serverlist creation form (empty)
    'forms': forms,  # these are all the forms for existing instances
    'posts': posts

我也看不到你在PostEdit 中的哪个位置指定了form_class,这是UpdateView 工作所必需的。

【讨论】:

在 form_class 下我写了: form_class= HomeForm 并且:对于帖子中的帖子:我需要在 post 函数中还是在 PostEdit 类中编写它?而模板渲染我应该把它写在serverlist.html中的modal中? 有两个form = HomeForm()..一个在第一行post下面一个在if form_is_valid()我应该改哪一个并放for循环? 我应该在我的模板中哪里添加这些行?因为现在它只显示第一个表单的数据 % with post.id as id % % with forms.id as form % do something with form % endwith % % endwith % 我假设你在某处循环你的posts% for post in posts % ... code to display your post and its edit form ... here you add the template code I mentioned ... % endfor % 我在最后的主要问题的表格中添加了我在 html 中循环的帖子的短代码...你能在答案中准确地编辑我应该添加 html 代码的位置吗?表格编号?还有什么是“做某事?”做什么?我只需要传递表单 id no?

以上是关于Django中的UpdateView显示空白表单而不是数据库以前的数据的主要内容,如果未能解决你的问题,请参考以下文章

Django 1.8 UpdateView 生成 405 错误和空白模板

Django 中 UpdateView 上的日期格式更改

基于 Django 类的视图 - 具有两个模型表单的 UpdateView - 一个提交

python Django(极端情况):如何在FormView(CreateView / UpdateView)的form_valid方法中引发表单无效并添加错误消息

在UpdateView上获取删除按钮以重定向时出现问题

使用基于类的 UpdateView 在 Django 中更新用户模型