Django-如何在表单中正确使用UpdateView?

时间:2019-01-15 03:02:36

标签: python django django-models django-forms

我当前的表单提交正确,但是在每次提交表单后在django管理员中添加其他列表。我上了一堂 SELECT attach_id,post_msg_id FROM (SELECT Distinct phpbb_attachments.attach_id, FROM phpbb_attachments INNER JOIN phpbb_topics ON phpbb_attachments.topic_id = phpbb_topics.topic_id INNER JOIN phpbb_forums ON phpbb_topics.forum_id = phpbb_forums.forum_id WHERE phpbb_attachments.is_orphan = 0 AND phpbb_attachments.thumbnail = 1 AND (phpbb_attachments.extension = 'jpg' OR phpbb_attachments.extension = 'png' OR phpbb_attachments.extension = 'gif') AND phpbb_forums.forum_id BETWEEN 668 AND 679 ORDER BY phpbb_attachments.attach_id DESC LIMIT 50) as firstDistinct LEFT JOIN (SELECT phpbb_attachments.attach_id, phpbb_attachments.post_msg_id FROM phpbb_attachments INNER JOIN phpbb_topics ON phpbb_attachments.topic_id = phpbb_topics.topic_id INNER JOIN phpbb_forums ON phpbb_topics.forum_id = phpbb_forums.forum_id WHERE phpbb_attachments.is_orphan = 0 AND phpbb_attachments.thumbnail = 1 AND (phpbb_attachments.extension = 'jpg' OR phpbb_attachments.extension = 'png' OR phpbb_attachments.extension = 'gif') AND phpbb_forums.forum_id BETWEEN 668 AND 679 ORDER BY phpbb_attachments.attach_id DESC LIMIT 50) as secondNotDistinct On firstDistinct.attach_id = secondNotDistinct.attach_id 类,以为这可以解决问题,而且没有运气。

由于我是Django的新手,所以我想念一些东西。

我只需要使用此表单即可根据当前用户更新django管理员中的值,而无需重复创建其他列表。

用当前代码执行此操作的最佳方法是什么?

我很感激任何帮助,干杯

user_profile / models

UpdateView

user_profile / views.py

from django.contrib import auth
from django.db import models
from django.urls import reverse
from django.contrib.auth.models import AbstractUser, UserManager
from django.contrib.auth.models import BaseUserManager
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from users.models import CustomUser

class Listing (models.Model):

    image = models.ImageField(default='default.jpg', upload_to='profile_pics')
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)
    created =  models.DateTimeField(auto_now_add=True, null=True)
    updated = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length=100)
    address = models.CharField(max_length=100)
    zip_code = models.CharField(max_length=100)
    mobile_number = models.CharField(max_length=100)

def create_profile(sender, **kwargs):
    if kwargs['created']:
        user_profile = Listing.objects.create(user=kwargs['instance'])

post_save.connect(create_profile, sender=CustomUser)

user_profile / urls.py

from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponseNotFound
from django.shortcuts import get_object_or_404
from django.shortcuts import render, redirect
from django.conf import settings
from django.views.generic.edit import FormView
from django.views.generic.edit import CreateView
from django.views.generic.edit import UpdateView
from django.views.generic import TemplateView
from .forms import HomeForm
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from .models import Listing
from users.models import CustomUser
from django.urls import reverse_lazy
from django.utils import six
from django.utils.translation import ugettext as _
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied

class change_view_update(LoginRequiredMixin, UpdateView):
    model = Listing    
    template_name_suffix = 'myaccount-update'    
    form_class = HomeForm
    success_url = '/home/'
    login_url = 'login'

    def dispatch(self, request, *args, **kwargs):
        obj = self.get_object()
        if obj.name != self.request.user:
            raise PermissionDenied
            return super().dispatch(request, *args, **kwargs)


class change_view(CreateView):
    template_name = 'myaccount.html'
    form_class = HomeForm
    model = Listing

    def form_valid(self, form):
        Listing = form.save(commit=False)
        Listing.user = self.request.user
        Listing.save()
        return redirect("myaccount")

0 个答案:

没有答案