【问题标题】:Can I have two edit profile forms in django One is the main edit Profile form and 1 for just 2 fields我可以在 django 中有两个编辑配置文件表单一个是主要的编辑配置文件表单,一个是 2 个字段
【发布时间】:2018-08-31 03:02:46
【问题描述】:

我有一个包含太多字段的配置文件模型。其中两个字段是latlon我使用django表单编辑下面的所有字段

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100, blank=True, null=True)
    country = models.CharField(max_length=100)
    street_address = models.CharField(max_length=500, null=True, blank=True)
    zip_code = models.CharField(max_length=10, default='')
    phone_number = models.CharField(max_length=17, blank=True, null=True)
    age = models.DateField(blank=True, null=True)
    member_since = models.DateTimeField(auto_now_add=True)
    profile_image = models.ImageField(default='', blank=True, null=True)
    bio = models.TextField(default='', blank=True)
    bio_images = models.ImageField(default='', blank=True, null=True)
    activities_i_do = models.TextField(default='', blank=True, null=True)
    activities_i_love = models.TextField(default='', blank=True, null=True)
    is_verified = models.BooleanField(default=False)
    lat = models.FloatField(blank=True, null=True)
    lon = models.FloatField(blank=True, null=True)

下面是编辑属性的视图

@login_required
def edit_profile(request):
    if request.method == 'POST':
        user_form = UserEditForm(data=request.POST or None, instance=request.user)
        profile_form = ProfileEditForm(data=request.POST or None, instance=request.user.profile, files=request.FILES)
        if user_form.is_valid() and profile_form.is_valid():
                user_form.save()
                profile_form.save()
            return redirect('accounts:profile', username=request.user.username)
    else:
        user_form = UserEditForm(instance=request.user)
        profile_form = ProfileEditForm(instance=request.user.profile)

    context = {'user_form': user_form,
               'profile_form': profile_form}
    return render(request, 'accounts/profile_edit.html', context)

现在的问题是我可以使用其他表单来编辑上述模型示例的latlon 字段,如下所示

        <form method="post" action="{{ ??????????? }}">
            <input id="jsLat" type="text" placeholder="latittude" >
            <input id="jsLon" type="text" placeholder="longitude">
            <button type="submit" id="submit">Submit</button>
        </form>

我已经使用 jquery 制作了表单,这样当用户单击找到我时,表单会自动填写并自行提交。我可以使用此表单仅编辑配置文件模型的 2 个字段

下面是我的个人资料编辑表格

class ProfileEditForm(forms.ModelForm): #UserProfileForm or ProfileEdit
    class Meta:
        model = Profile
        fields = ('city', 'state', 'country', 'street_address', 'zip_code', 'phone_number', 'age', 'profile_image',
                  'bio', 'bio_images', 'activities_i_do', 'activities_i_love', 'lat', 'lon')

PS:为了简单起见,geodjango 代码已被删除。只需要更新latlon,就好像它们是常规字段一样

【问题讨论】:

    标签: django python-3.x django-models django-forms django-views


    【解决方案1】:

    您不需要创建另一个表单。由于您没有显示您的表格,我会假设您使用过

    class ProfileForm(forms.ModelForm):
        class Meta:
            model = Profile
            fields = ('user', 'city', 'state'...)
    

    你可以做的是渲染 profile_form 并像这样使用它

        <form method="post" action="{{ ??????????? }}">
            <input id="jsLat" type="text" placeholder="latittude" value="{{profile_form.lat.value}}">
            <input id="jsLon" type="text" placeholder="longitude" value="{{profile_form.lon.value}}">
            <button type="submit" id="submit">Submit</button>
        </form>
    

    在你的意见.py

    profile_form = ProfileEditForm(request.POST)
    profile_form.save()
    

    希望它对您有所帮助或给您一个想法!当然,您需要在表单中插入某种主键或 ID,以便获得要编辑的配置文件的上下文。

    【讨论】:

    • 我要在操作字段中添加什么?我也添加了配置文件编辑表单。就像你展示的那样
    • 这取决于你,但我会再次做出假设。如果“longlat”表单与个人资料表单位于不同的页面上,您必须创建一个新视图来处理该表单。
    【解决方案2】:

    似乎很难为 1 个对象设置 2 个编辑表单。所以我拆分了latlon,并为它制作了一个新模型

    class UserLocation(models.Model):
        user = models.ForeignKey(User, on_delete=models.CASCADE)
        lat = models.FloatField(blank=True, null=True)
        lon = models.FloatField(blank=True, null=True)
    

    然后我使用上面相同的 HTML 表单在下面的问题中编辑此对象详细信息

    Editing an object with HTML form instead of Django model form

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 2018-10-05
      • 2022-10-05
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2012-11-04
      相关资源
      最近更新 更多