I am trying to allow a user to modify their profile.
On the right we see how the user has two currencies associated: Bitcoin and Ethereum and an exchange house: Coinbase
The objective is that in the form on the left the user can change their data and add or remove coins and purses (exchange houses).
Needed:
- Know how to manage that with ModelMultipleChoiceField fields
- How to show the current value in the fields, for example in the Name input it should show antonigalile and in the email [email protected]
I would like the coin and wallet selector to look like this:
Here are my files:
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 (still unfinished)
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')
What I can do? Thank you
To my question about how to manage coin selection with a ManyToMany form:
Pressing CTRL + left clicking on the chosen currency is how they are selected.
To my question about making the value that the user has in your database appear by default in the form:
solved from jquery