gpt4 book ai didi

python - django-admin:如何在保存一个对象后重定向到 URL?

转载 作者:太空宇宙 更新时间:2023-11-03 14:33:14 27 4
gpt4 key购买 nike

创建用户后,我将使用 Django 信号来触发代码,我正在另一个模型类上保存附加数据,它会被触发,但不会重定向到附加数据对象页面。

这是我的 models.py

from django.db import models
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class Customers(models.Model):
user = models.OneToOneField(User)
business_name = models.CharField(max_length=250)
address = models.CharField(max_length=500)
area = models.CharField(max_length=250)
city = models.CharField(max_length=250)
state = models.CharField(max_length=250)
pincode = models.IntegerField(default='0')
phone = models.IntegerField(default='0')
mobile = models.IntegerField(default='0')

def create_customer(sender, **kwargs):
if kwargs['created']:
customer_profile = Customers.objects.create(user=kwargs['instance'])

post_save.connect(create_customer, sender=User)

这是我的 admin.py

from django.contrib import admin
from .models import Customers
from django.shortcuts import redirect

admin.site.register(Customers)

class Customers(admin.ModelAdmin):
def response_add(self, request, obj, post_url_continue=None):
return redirect('/admin/app/customers/add/')

def response_change(request, obj):
return redirect('/admin/app/customers/add/')

厌倦了寻找答案,但没有任何效果,请在这里纠正我。

最佳答案

您似乎没有注册您的管理配置。引用Django docs on the register decorator了解更多信息。语法是用 @admin.register(<model>) 修饰你的函数。或者如果您想手动注册模型管理配置,则 admin.site.register(<model>, <model admin>) .

我建议更改 class Customers(admin.ModelAdmin):class CustomersAdmin(admin.ModelAdmin):或类似的东西来阻止命名空间冲突。

参见下面的示例:

from django.contrib import admin
from .models import Customers
from django.shortcuts import redirect


@admin.register(Customers)
class CustomersAdmin(admin.ModelAdmin):
def response_add(self, request, obj, post_url_continue=None):
return redirect('/admin/app/customers/add/')

def response_change(request, obj):
return redirect('/admin/app/customers/add/')

关于python - django-admin:如何在保存一个对象后重定向到 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47126983/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com