Django 网站 - TemplateDoesNotExist 在 /edit/117/

Posted

技术标签:

【中文标题】Django 网站 - TemplateDoesNotExist 在 /edit/117/【英文标题】:Django Website - TemplateDoesNotExist at /edit/117/ 【发布时间】:2018-05-10 18:26:06 【问题描述】:

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

单击“编辑”按钮后,会弹出一个模式,其中包含一些可以编辑的表单,然后在我编辑并单击“确认”后出现错误:

TemplateDoesNotExist at /edit/117/ (or other pk...)

DevOpsWeb/serverlist_form.html
Request Method: POST
Request URL:    http://devopsweb:8000/edit/117/
Django Version: 1.11.6
Exception Type: TemplateDoesNotExist
Exception Value:    
DevOpsWeb/serverlist_form.html
    为什么会出现此错误? 为什么我拿到modal的时候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="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 %

【问题讨论】:

模板中有serverlist_form.html 文件吗? 不,我从来没有这个模板......但问题是我使用的是一个类似于弹出窗口的模式......然后当我点击确认它应该重定向到相同页。我的编辑以前可以工作..我做了一些更改,从那以后我遇到了这个错误.. 现在运行完美。你知道在我点击编辑而不是空白表单之前如何获取数据吗? 【参考方案1】:

由于您没有覆盖UpdateView 属性、template_name_suffixtemplate_nameget_templates_names() 之一,因此您的视图将查看的默认模板名称是&lt;model_name&gt;_form.html。而你没有那个模板

【讨论】:

没错。我没有那个模板,因为我使用的是同一个页面,只是弹出一个模式(一个小窗口),在我按下确认后它应该带我回到同一个页面,我点击了按钮。我应该在视图中添加什么?我应该覆盖哪个属性? @EilonAshkenazi 尝试使用template_name = 'index.html' 在我点击确认后在 UpdateVIew 下添加后,我进入 devopsweb/edit/117 并成功显示主页但没有任何数据...我尝试添加 - 帖子 = serverlist.objects。 all() 但它仍然没有显示任何内容,并且主页在没有任何数据库条目的情况下工作..就像它是空的..也许我需要另一个 Get 请求?就像也许我应该以某种方式调用 get 函数来在编辑后获取所有数据库? 另外,为什么在 PostDelete 下我不需要做 template_name?删除后成功将我重定向到主页 @EilonAshkenazi 因为它只是一个不需要模板的发布方法,它只是发送请求并重定向到提供的success_url

以上是关于Django 网站 - TemplateDoesNotExist 在 /edit/117/的主要内容,如果未能解决你的问题,请参考以下文章

Django搭建博客网站

Django搭建博客网站

Django总结用Django框架搭建表白网站

Django搭建博客网站

Django搭建博客网站

如何使用wordpress + django搭建网站