Django:ModelForm并在同一个url上显示数据

时间:2018-05-13 12:30:59

标签: python django

我是django的新手,并试图创建我的第一个应用程序,我想我可能需要一些帮助:)

我在网站上有一个要提交的ModelForm,并希望在同一页面上显示数据。我在同一页面上设置两个函数时遇到问题,我想我可能不得不使用一个类并在urls.py中设置它但我无法使它工作:(代码看起来像这样:

forms.py:

from django import forms
from .models import Eintrag

class NameForm(forms.ModelForm):

    class Meta:
            model = Eintrag
            fields = ['Anmeldung', 'Essen']

urls.py

from django.urls import path

from . import views

app_name = 'form'
urlpatterns = [
    path('', views.get_name, name='form'),
]

views.py

from django.shortcuts import render
from django.utils import timezone
from django.contrib.auth.decorators import login_required

from .forms import NameForm
from .models import Eintrag

@login_required()
def get_name(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = NameForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            eintrag = form.save(commit=False)
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            eintrag.Name = request.user  # Set the user object here
            eintrag.pub_date = timezone.now()  # Set the user object here
            eintrag.save()

            return render(request, 'form/name.html', {'form': form})

    # if a GET (or any other method) we'll create a blank form
    else:
        form = NameForm()

    return render(request, 'form/name.html', {'form': form})

def post_list(request):
    posts = Eintrag.objects.all()
    return render('form/post_list.html', {'posts': posts})

name.html

...
{% include "form/post_list.html" %}

<form action="/form/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>
...

post_list.html

{% for post in posts %}
    {{ post }}
{% endfor %}

所以问题出在urls.py中,只处理了get_name,而且我不知道如何包含post_list。我宁愿不想使用不同的网址,我必须这样做吗?

感谢您提供任何帮助和建议!

2 个答案:

答案 0 :(得分:2)

列表中不需要单独的URL或视图。只需在get_name视图的上下文中包含查询集。

posts = Eintrag.objects.all()
return render(request, 'form/name.html', {'form': form, 'posts': posts})

答案 1 :(得分:1)

使用[基于类的视图]会更好。
但是,使用view,您可以通过context发送多个数据。

@login_required()
def get_name(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        ''' codes '''
        eintrag.save()

        return HttpResponseRedirect(request.path) # generate an empty form

    # if a GET (or any other method) we'll create a blank form
    else:
        form = NameForm()

    posts = Eintrag.objects.all() # the queryset is here, and sent via context
    return render(request, 'form/name.html', {'form': form,'posts':posts})

我的html保持不变,但保持您的表单action=''为空

{% include "form/post_list.html" %}

<form action="" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>