Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数def post中的两种形式_Python_Django_Forms_Post - Fatal编程技术网

Python 函数def post中的两种形式

Python 函数def post中的两种形式,python,django,forms,post,Python,Django,Forms,Post,我有一个编写的函数def post,用于在帖子中添加评论。在本例中,当我发送一个费率表时,不会向数据库中添加任何记录。我需要编辑此功能,以便当我发送带有评级帖子的表单时,表单中的所有数据都会保存在数据库中。在数据库中的形式,我需要保存的评级,用户谁评级,日期和职位的评级值 只是我需要在发送表单后将值添加到数据库中 视图.py: class IndexView(AllAdsViewMixin, ListView): model = UserContent template_name

我有一个编写的函数
def post
,用于在帖子中添加评论。在本例中,当我发送一个费率表时,不会向数据库中添加任何记录。我需要编辑此功能,以便当我发送带有评级帖子的表单时,表单中的所有数据都会保存在数据库中。在数据库中的形式,我需要保存的评级,用户谁评级,日期和职位的评级值

只是我需要在发送表单后将值添加到数据库中

视图.py

class IndexView(AllAdsViewMixin, ListView):
    model = UserContent
    template_name = 'user_content/list.html'
    context_object_name = 'object'

    def get_queryset(self):
        """Return the last all five published contents"""
        return UserContent.objects.filter(state='1').order_by('-published')[:5]

    def get_context_data(self, **kwargs):
        comment_initial_data = {
            'content_type': ContentType.objects.get_for_model(self.model).id
        }

        reply_initial_data = {
            'content_type': ContentType.objects.get_for_model(Comment).id
        }

        comment_form = CommentForm(initial=comment_initial_data)
        reply_form = CommentForm(initial=reply_initial_data)
        mainpage_images = MainPageImages.objects.first()
        rate_form = RateForm()
        context = super(IndexView, self).get_context_data(**kwargs)
        context['comment_form'] = comment_form
        context['reply_form'] = reply_form
        context['mainpage_images'] = mainpage_images
        context['rate_form'] = rate_form

        return context

    def post(self, request, **kwargs):
        comment_form = CommentForm(request.POST)

        if comment_form.is_valid():
            content_type_id = comment_form.cleaned_data.get('content_type')
            object_id_data = comment_form.cleaned_data.get('object_id')
            content_data = comment_form.cleaned_data.get('content')

            content_type = ContentType.objects.get_for_id(content_type_id)
            print(object_id_data)
            print(content_data)

            new_comment, created = Comment.objects.get_or_create(
                user=request.user,
                content_type=content_type,
                object_id=object_id_data,
                content=content_data,
            )

        else:
            print(comment_form.errors)

        return self.get(request, **kwargs)
from django import forms
from .models import Rate


class RateForm(forms.ModelForm):
    class Meta:
        model = Rate
        fields = (
          'rate_value',
        )
from django.db import models
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from user_content.models import UserContent


class Rate(models.Model):
    user_content = models.ForeignKey(UserContent,
                                     related_name='rates',
                                     verbose_name=_('User Content')
                                     )
    rate_value = models.DecimalField(decimal_places=2, max_digits=3,
                                     verbose_name=_('rates'))
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             on_delete=models.CASCADE,
                             verbose_name=_('User')
                             )
    created = models.DateTimeField(_('Date Created'), auto_now_add=True)
forms.py

class IndexView(AllAdsViewMixin, ListView):
    model = UserContent
    template_name = 'user_content/list.html'
    context_object_name = 'object'

    def get_queryset(self):
        """Return the last all five published contents"""
        return UserContent.objects.filter(state='1').order_by('-published')[:5]

    def get_context_data(self, **kwargs):
        comment_initial_data = {
            'content_type': ContentType.objects.get_for_model(self.model).id
        }

        reply_initial_data = {
            'content_type': ContentType.objects.get_for_model(Comment).id
        }

        comment_form = CommentForm(initial=comment_initial_data)
        reply_form = CommentForm(initial=reply_initial_data)
        mainpage_images = MainPageImages.objects.first()
        rate_form = RateForm()
        context = super(IndexView, self).get_context_data(**kwargs)
        context['comment_form'] = comment_form
        context['reply_form'] = reply_form
        context['mainpage_images'] = mainpage_images
        context['rate_form'] = rate_form

        return context

    def post(self, request, **kwargs):
        comment_form = CommentForm(request.POST)

        if comment_form.is_valid():
            content_type_id = comment_form.cleaned_data.get('content_type')
            object_id_data = comment_form.cleaned_data.get('object_id')
            content_data = comment_form.cleaned_data.get('content')

            content_type = ContentType.objects.get_for_id(content_type_id)
            print(object_id_data)
            print(content_data)

            new_comment, created = Comment.objects.get_or_create(
                user=request.user,
                content_type=content_type,
                object_id=object_id_data,
                content=content_data,
            )

        else:
            print(comment_form.errors)

        return self.get(request, **kwargs)
from django import forms
from .models import Rate


class RateForm(forms.ModelForm):
    class Meta:
        model = Rate
        fields = (
          'rate_value',
        )
from django.db import models
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from user_content.models import UserContent


class Rate(models.Model):
    user_content = models.ForeignKey(UserContent,
                                     related_name='rates',
                                     verbose_name=_('User Content')
                                     )
    rate_value = models.DecimalField(decimal_places=2, max_digits=3,
                                     verbose_name=_('rates'))
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             on_delete=models.CASCADE,
                             verbose_name=_('User')
                             )
    created = models.DateTimeField(_('Date Created'), auto_now_add=True)
型号.py

class IndexView(AllAdsViewMixin, ListView):
    model = UserContent
    template_name = 'user_content/list.html'
    context_object_name = 'object'

    def get_queryset(self):
        """Return the last all five published contents"""
        return UserContent.objects.filter(state='1').order_by('-published')[:5]

    def get_context_data(self, **kwargs):
        comment_initial_data = {
            'content_type': ContentType.objects.get_for_model(self.model).id
        }

        reply_initial_data = {
            'content_type': ContentType.objects.get_for_model(Comment).id
        }

        comment_form = CommentForm(initial=comment_initial_data)
        reply_form = CommentForm(initial=reply_initial_data)
        mainpage_images = MainPageImages.objects.first()
        rate_form = RateForm()
        context = super(IndexView, self).get_context_data(**kwargs)
        context['comment_form'] = comment_form
        context['reply_form'] = reply_form
        context['mainpage_images'] = mainpage_images
        context['rate_form'] = rate_form

        return context

    def post(self, request, **kwargs):
        comment_form = CommentForm(request.POST)

        if comment_form.is_valid():
            content_type_id = comment_form.cleaned_data.get('content_type')
            object_id_data = comment_form.cleaned_data.get('object_id')
            content_data = comment_form.cleaned_data.get('content')

            content_type = ContentType.objects.get_for_id(content_type_id)
            print(object_id_data)
            print(content_data)

            new_comment, created = Comment.objects.get_or_create(
                user=request.user,
                content_type=content_type,
                object_id=object_id_data,
                content=content_data,
            )

        else:
            print(comment_form.errors)

        return self.get(request, **kwargs)
from django import forms
from .models import Rate


class RateForm(forms.ModelForm):
    class Meta:
        model = Rate
        fields = (
          'rate_value',
        )
from django.db import models
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from user_content.models import UserContent


class Rate(models.Model):
    user_content = models.ForeignKey(UserContent,
                                     related_name='rates',
                                     verbose_name=_('User Content')
                                     )
    rate_value = models.DecimalField(decimal_places=2, max_digits=3,
                                     verbose_name=_('rates'))
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             on_delete=models.CASCADE,
                             verbose_name=_('User')
                             )
    created = models.DateTimeField(_('Date Created'), auto_now_add=True)

我们需要查看表单才能为您提供答案。添加了models.py和forms.py。对不起,我的错。