没有模板渲染的 Django CreateView

Django CreateView without template rendering

我在 CreateView 中保存表单时遇到问题,我发现 这个解决方案对我有用:

class ScheduleDocumentView(CreateView):

    def post(self, request, pk, *args, **kwargs):
        form = ScheduleDocumentForm(request.POST, request.FILES)
        if form.is_valid():
            form.instance.relates_to = Schedule.objects.get(pk=pk)
            form.save()
        return redirect('planning:schedule-detail', pk=pk)

但是我的目标是在 CreateView 中使用 form_valid() 和 get_success_url() 来保存没有模板的表单。我试过这样的东西(不起作用):

class ScheduleDocumentView(CreateView):
    model = ScheduleDocument
    form_class = ScheduleDocumentForm

    def form_valid(self, form):
        form.instance.relates_to = Schedule.objects.get(pk=pk)
        form.save()
        return redirect('planning:schedule-detail', pk=pk)

    def get_success_url(self):
        return reverse('planning:schedule-detail', kwargs={'pk': pk})

它需要一个模板,是否有任何其他方法可以处理我在 DetailView 中的 post 请求,在单独的 CreateView 中处理它并将其重定向到我的 DetailView 页面?

这是我的 DetailView 模板:

<form enctype="multipart/form-data" action="{% url 'planning:upload-document' schedule.pk %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="button button--secondary">Attach</button>

网址:

path('schedules/<int:pk>/', ScheduleDetailView.as_view(), name='schedule-detail'),
path('schedules/<int:pk>/upload-document/', ScheduleDocumentView.as_view(), name='upload-document'),

我遇到了这个解决方案:

class ScheduleDocumentView(CreateView):
    model = ScheduleDocument
    form_class = ScheduleDocumentForm

    def form_valid(self, form):
        form.instance.schedule = Schedule.objects.get(pk=self.kwargs['pk'])
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('planning:schedule-detail', kwargs={'pk': self.kwargs['pk']})

template_name is required Django Docs:

The full name of a template to use as defined by a string. Not defining a template_name will raise a django.core.exceptions.ImproperlyConfigured exception.

或者在你的情况下 Django 会导致默认的 template_name'yourapp/scheduledocument_create_form.html'.

因此您得到错误 TemplateDoesNotExist

您可以从 self.kwargs(Django Docs) 中获取 pk 值。

您可以简单地创建 blank.html 模板。

class ScheduleDocumentView(CreateView):
    http_method_names = ['post']
    template_name = 'blank.html' # or use this 'schedule_detail.html'
    model = ScheduleDocument
    form_class = ScheduleDocumentForm

    def form_valid(self, form):
        form.instance.relates_to = Schedule.objects.get(pk=self.kwargs.get("pk"))
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('planning:schedule-detail', kwargs={'pk': self.kwargs.get("pk")})

或使用A simple view:

def create_schedule_document(request, pk):
    if request.method == 'POST':
        form = ScheduleDocumentForm(request.POST, request.FILES)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.relates_to = Schedule.objects.get(pk=pk)
            obj.save()
    else:
        form = ApplyAnonymousForm()
    return redirect('planning:schedule-detail', pk=pk)