我的html中有一个ModelManyToMany选择器。
(我无法发布图片,所以这是图片的网址:https://i.stack.imgur.com/HDhde.png)
我想在自己的views.py
中使用POST方法捕获用户的选择。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='edit')
return context
def post(self, request, *args, **kwargs):
edit_name = request.POST.get('edit-name')
edit_two_factors_auth = request.POST.get('edit-two_factors_auth')
edit_coins = request.POST.get('edit-coins')
if request.method == "POST":
if 'profileButton' in request.POST:
if edit_name and (edit_name != request.user.name):
request.user.name = edit_name
request.user.save()
print(edit_coins)
return render(request, 'user.html')
models.py
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)
我的views.py中的打印仅返回我最后的选择,而不是用户完成的所有选择。
我要保存用户在他的硬币字段中选择的所有硬币
答案 0 :(得分:0)
您必须更改此行
edit_coins = request.POST.get('edit-coins')
到
edit_coins = request.POST.getlist('edit-coins')
及其作品。之后,edit_conis是一个列表,其中包含用户选择的所有项目。