【问题标题】:creating edit page for profile in django在 django 中为个人资料创建编辑页面
【发布时间】:2015-05-13 05:30:53
【问题描述】:

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

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')

我只得到更新按钮。帮帮我……

【问题讨论】:

  • 请正确格式化您的代码,缩进无处不在。

标签: python django edit profile


【解决方案1】:

首先,由于您的配置文件模型中有ForeignKeys 到User 模型,因此每个User 可以有多个配置文件。如果您希望它是一个单一的,请将它们更改为:

user = models.OneToOneField(User)

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

instance = request.user.company_profile

instance = request.user.user_profile

【讨论】:

  • 我更改为 OnetoOnefield ,并且 instance = request.user.company_profile 现在我只得到更新按钮没有别的。我将编辑我的代码
  • 你能把update_company_prof的代码也发一下吗
  • 您的update_company_prof 必须扩展forms.ModelForm,而不是UpdateView。还可以考虑将 update_company_prof 的名称更改为 CompanyProfileForm 之类的名称,因为它目前非常具有误导性。
  • 徽标显示为链接。我应该怎么做才能让照片出现。
  • 您无法在表单中修复它,但您可以在模板的其他位置放置一个 img 标签:&lt;img src="{{ user.company_profile.photo.url }}"&gt;
【解决方案2】:

您的 update_company_prof 必须扩展 forms.ModelForm,而不是 UpdateView。还可以考虑将 update_company_prof 的名称更改为 CompanyProfileForm 之类的名称,因为它目前非常具有误导性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-30
    • 2012-02-21
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多