Django-表单未更新

时间:2019-01-10 02:04:09

标签: python django

我正在Web应用程序中创建个人资料页面,并且有一个表单,用于在用户提交表单时需要表单中的数据,以便从当前登录用户的admin中更新表单。

该数据填充在管理员中,但是每次用户提交表单时,新列表都会重复。我只需要更新数据。截图。

如何使用当前代码正确执行此操作?

如果有帮助,我正在使用的自定义用户模型位于from users.models import CustomUser中。

我很感激任何帮助,干杯

enter image description here

enter image description here

user_profile / models

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)
#    cc_number = models.CharField(max_length=100)
#    cc_expiration = models.CharField(max_length=100)
#    cc_cvv = models.CharField(max_length=100)
#    objects = ListingManager()

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 / views.py

def change_view(request):
    form = HomeForm(request.POST or None, request.FILES or None)
    user_profile = Listing.objects.all
    user = request.user
    if request.method == "POST":  # checking if request is POST or Not
        # if its a post request, then its checking if the form is valid or not
        if form.is_valid():
            listing_instance = form.save(commit=False)  # "this will return the 'Listing' instance"
            listing_instance.user = user # assign 'user' instance
            listing_instance.save() # calling 'save()' method of model
            return redirect("myaccount")

    context = {
        'form': form, 'user_profile': user_profile 
    }

    return render(request, "myaccount.html", context)

user_profile / admin.py

from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin


from user_profile.forms import HomeForm
from users.forms import CustomUserCreationForm, CustomUserChangeForm

from user_profile.models import Listing
from users.models import CustomUser


# Register models here.
class UserProfileAdmin(admin.ModelAdmin):
    list_display = ['name', 'address', 'zip_code', 'mobile_number', 'created', 'updated', 'user']
    list_filter = ['name', 'zip_code', 'created', 'updated', 'user']

admin.site.register(Listing, UserProfileAdmin)

html

{% block content %}
<form role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.errors }}   
{{ form.name }}
{{ form.address }}
{{ form.zip_code }}
{{ form.mobile_number }}
{{ form.image }}
<button class="btn btn-primary btn-success btn-round btn-extend" type="submit" value="Submit"><i class="zmdi zmdi-favorite-outline6"></i>Submit</button>
</form>    
{% endblock content %}

user_profile / urls.py

from django.conf.urls import url
from . import views
from django.urls import path, include
from django.conf import settings

from .views import change_view

urlpatterns = [
    path('myaccount/', change_view, name='myaccount'),
]

设置

AUTH_USER_MODEL = 'users.CustomUser'

3 个答案:

答案 0 :(得分:1)

newtype Sum a = Sum { getSum :: a }

x :: Sum Int
x = Sum 1

y :: Int
y = getSum x

-- the alternative without record syntax:
newtype Sum' a = Sum' a

getSum' :: Sum' a -> a
getSum' (Sum' x) = x

x' :: Sum' Int
x' = Sum' 1

y' :: Int
y' = getSum' x

用于更改视图功能

 pass customer Id after request and use url path as mentioned below 

答案 1 :(得分:0)

将此行listing_instance = form.save()更改为

listing_instance = form.save(commit=False)

答案 2 :(得分:0)

实际上,我认为问题在于您没有区分请求类型。您应该在视图中分别处理获取和发布请求。因此视图应如下所示:

def change_view(request):
    user = request.user
    user_profile = Listing.objects.filter(user=user).first()
    form = HomeForm(request.POST or None, request.FILES or None, instance=user_profile)
    if request.method == "POST":  # checking if request is POST or Not
        # if its a post request, then its checking if the form is valid or not
        if form.is_valid():
            listing_instance = form.save(commit=False)  # "this will return the 'Listing' instance"
            listing_instance.user = user # assign 'user' instance
            listing_instance.save() # calling 'save()' method of model
            return redirect("success-url-path")

    context = {
        'form': form
    }

    return render(request, "myaccount.html", context)