如何显示多方django表单

时间:2018-07-06 20:10:07

标签: python django django-models django-forms

我正在尝试使用户可以修改其个人资料。

enter image description here

在紧要关头(在url中所附的图像中),我们可以看到用户拥有硬币和钱包(比特币,以太坊和coinbase)

目标是在左侧的表单中,用户可以更改其数据并添加或删除硬币和钱包。

我需要:

  1. 如何使用 ModelMultipleChoiceField 做到这一点?
  2. 加载表单时如何在输入中显示用户数据?例如:antonigalile将是未输入名称的

我希望我的小部件看起来像这样:

enter image description here

这是我的文件:

models.py

from django.db import models
from django.utils.timezone import now
from django.contrib.auth.models import AbstractUser


class Coin(models.Model):
    name = models.CharField(unique=True,max_length=50)
    url = models.CharField(max_length=200)
    transaction_fee = models.FloatField(default=0.0,blank=True)
    price = models.FloatField(default=0.0,blank=True)
    market_cap = models.IntegerField(default=0,blank=True)
    volume = models.IntegerField(default=0,blank=True)
    circulating = models.IntegerField(default=0,blank=True)
    change = models.FloatField(default=0.0,blank=True)
    def __str__(self): 
        return self.name

class Wallet(models.Model):
    name = models.CharField(unique=True,max_length=50)
    url = models.CharField(max_length=200)
    coins = models.ManyToManyField(Coin)
    def __str__(self): 
        return self.name

class Usuario(AbstractUser):
    name = models.CharField(max_length=12, help_text="The name must be between 2 and 12 characters")
    email = models.EmailField(max_length=60, unique=True, help_text="The email must be between 5 and 30 characters")
    password = models.CharField(max_length=78)
    change_password_code = models.CharField(blank=True,max_length=15)
    activated = models.BooleanField(default=False)
    activated_code = models.CharField(default="",max_length=15)
    ip = models.CharField(blank=True,max_length=15)
    last_login = models.DateField(default=now)
    wallets = models.ManyToManyField(Wallet)
    coins = models.ManyToManyField(Coin)
    avatar = models.CharField(blank=True,default="bitcoin.png",max_length=15)
    delete_code = models.CharField(default="",max_length=9,blank=True)
    two_factors_auth = models.BooleanField(default=False)
    two_factors_auth_code = models.CharField(default="",max_length=12,blank=True)
    fingerprint = models.CharField(max_length=64,blank=True)

forms.py

class EditProfileForm(forms.ModelForm):
    class Meta:
        model = Usuario
        exclude = ()
    wallets = forms.ModelMultipleChoiceField(queryset=Wallet.objects.all(),to_field_name="name")
    name = forms.CharField(label='Name')
    email = forms.EmailField(label='Email')
    two_factors_auth = forms.BooleanField()
    coins = forms.ModelMultipleChoiceField(queryset=Coin.objects.all(),to_field_name="name")

form.html

<form method="post">
     {% csrf_token %}
     <div class="form-group">
         <p> {{ edit_profile_form.name.label }} </p>
         {% render_field edit_profile_form.name class+="form-control" %}
         {{ edit_profile_form.name.errors }}
     </div>
     <div class="form-group">
         <p> {{ edit_profile_form.email.label }} </p>
         {% render_field edit_profile_form.email class+="form-control" %}
         {{ edit_profile_form.email.errors }}
     </div>
     <div class="form-group">
         <p>My Wallets</p>
         {{ edit_profile_form.wallets}}
         {{ edit_profile_form.wallets.errors }}
     </div>
     <div class="form-group">
         <p>My Coins</p>
         {{ edit_profile_form.coins }}
         {{ edit_profile_form.coins.errors }}
     </div>
     <div class="form-group">
        <p>Activate two factor authentication
        {{ edit_profile_form.two_factors_auth }}</p>
        {{ edit_profile_form.two_factors_auth.errors }}
    </div>
    <button type="submit" name="profileButton" class="btn btn-primary">Save</button>
</form>

views.py (未完成)

class userPageView(TemplateView):
template_name = 'user.html'

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['edit_profile_form'] = EditProfileForm(prefix='contact')
    return context

def post(self, request, *args, **kwargs):
    return render(request, 'user.html')

¿我该怎么办? 谢谢

0 个答案:

没有答案