python - 在 django 中为配置文件创建编辑页面

标签 python django edit profile

我是初学者。我的项目是关于公司和客户的。我为他们创建了个人资料页面。现在我想创建一个编辑页面,以便公司可以编辑他们的个人资料。 我的模型是:

class Company_Profile(models.Model):
    user = models.OneToOneField(User)
    name = models.CharField(_('Company Name'), max_length= 30)
    logo = models.FileField(_('Company Logo'), upload_to=get_upload_file_name, null=True, blank=True)
    address = models.TextField(_('Contact Address'), max_length=50)
    phone_no = models.IntegerField(_('Contact No'), max_length=12) 

class Customer_Profile(models.Model):
    user = models.OneToOneField(User)
    first_name = models.CharField(_('First Name'), max_length= 30)
    middle_name = models.CharField(_('Middle Name'), max_length= 30,null = True,blank=True)
    last_name = models.CharField(_('Last Name'), max_length= 30)
    photo = models.ImageField(_('Photo'), upload_to=get_upload_file_name, null=True, blank=True)
    address = models.TextField(_('Contact Address'), max_length=50)
    phone_no = models.IntegerField(_('Contact No'), max_length=12)

我的views.py是:

def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
           user = User.objects.create_user(
           username=form.cleaned_data['username'],
           password=form.cleaned_data['password1'],
           email=form.cleaned_data['email']
            )
           return HttpResponseRedirect('/login/')
    else:
        form = RegistrationForm()
    catagories = Company_Profile.objects.all()
    customers = Customer_Profile.objects.all()
    return render_to_response('index1.html',
                          {'form': form, 'catagories': catagories,     'customers' : customers},RequestContext(request))

def edit_company(request, offset):
    if request.method == 'POST':
       company_edit = update_company_prof(request.POST, instance = request.user.company_profile)
       if company_edit.is_valid():
           company_edit.save()
           return HttpResponseRedirect('company/'+str(company_edit.id))
    else:
       company_edit = update_company_prof(instance = request.user.company_profile)
    return render_to_response('edit_comp_prof.html', {'company_edit':   company_edit}, context_instance=RequestContext(request))

我的编辑html页面是:

<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
{{ company_edit.as_p }}
<input type="submit" value="Update" />
 <input type="reset" class="btn" value="cancel">
</form>

我的 update_comp_prof 表单是:

class update_company_prof(UpdateView):
    class Meta:
       model = Company_Profile
       fields = ('name','logo','address','phone_no')

我只得到更新按钮,没有其他任何东西。帮助我...

最佳答案

首先,由于您的个人资料模型中的 User 模型具有 ForeignKey,因此每个 User 可以有多个个人资料>。如果您希望它是单个的,请将它们更改为:

user = models.OneToOneField(User)

然后您可以使用型号名称访问您的个人资料,例如:

instance = request.user.company_profile

instance = request.user.user_profile

关于python - 在 django 中为配置文件创建编辑页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28986401/

相关文章:

python - 包含 Unicode 字符的单词不会出现在 HTML 输出中

python - 将状态设置为与 dbus 同理心时出错

Javascript 实时语音流并在 django 后端处理它

python - Sentry django 配置 - 记录器

css - 是否可以自定义 Bootstrap?

python - users().messages().list() 似乎上限为 500 条消息

python - 如何将 seaborn/matplotlib 轴刻度标签从数字格式化为数千或数百万? (125,436 至 125.4K)

javascript - 无法加载模块脚本 : The server responded with a non-JavaScript MIME type of "text/plain". 严格的 MIME 类型检查 i

c++ - 约束编辑控件内容

c# 从另一个表单更改已经打开的表单?