提问人:bld-sync 提问时间:11/17/2023 最后编辑:bld-sync 更新时间:11/17/2023 访问量:29
如何使用用户内置模型作为其他表单的默认值?
How to use the User built in model as a default value of another form?
问:
我正在做一个处理内置用户模型的项目。我有一个课程向我展示了如何使用用户模型......注册、登录/注销、仪表板、能够更新用户的个人资料和删除帐户。但是我无法让它以其他方式使用登录用户进行点击。
该应用程序的目标是
- 登录用户可以“创建”套牌,“用户”字段会自动设置为登录用户。更新目前,除非从管理面板填写,否则表单不会保存任何内容。
views.py
def my_decks(request):
deck_list = Deck.objects.filter(user=request.user)
user = User.objects.get(username=request.user)
if request.method == "POST":
deck = DeckForm(request.POST, instance=request.user)
deck.user = User.objects.get(username=request.user)
if deck.is_valid():
deck.save()
return HttpResponseRedirect('/my-decks')
else:
deck = DeckForm()
context = {'deck': deck, 'deck_list': deck_list, 'user': user}
return render(request, 'gametracker_app/my-decks.html', context=context)
---
# All below was from the class and works
def register(request):
form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
current_user = form.save(commit=False)
form.save()
profile = Profile.objects.create(user=current_user)
return redirect('my-login')
context = {'form': form}
return render(request, 'gametracker_app/register.html', context=context)
def my_login(request):
form = LoginForm()
if request.method == 'POST':
form = LoginForm(request, data=request.POST)
if form.is_valid():
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
auth.login(request, user)
return redirect('dashboard')
else:
messages.error(request, 'Invalid Login')
context = {'form': form}
return render(request, 'gametracker_app/my-login.html', context=context)
def user_logout(request):
auth.logout(request)
return redirect('')
def dashboard(request):
profile_pic = Profile.objects.get(user=request.user)
context = {'profilePic': profile_pic}
return render(request, 'gametracker_app/dashboard.html', context=context)
def profile_management(request):
# prepopulated fields
user_form = UpdateUserForm(instance=request.user)
profile = Profile.objects.get(user=request.user)
profile_form = UpdateProfilePicForm(instance=profile)
if request.method == 'POST':
user_form = UpdateUserForm(request.POST, instance=request.user)
profile_form = UpdateProfilePicForm(request.POST, request.FILES, instance=profile)
if user_form.is_valid():
user_form.save()
return redirect('dashboard')
if profile_form.is_valid():
profile_form.save()
return redirect('dashboard')
context = {'user_form': user_form, 'profile_form': profile_form}
return render(request, 'gametracker_app/profile-management.html', context=context)
def delete_account(request):
if request.method == 'POST':
delete_user = User.objects.get(username=request.user)
delete_user.delete()
return redirect("")
return render(request, 'gametracker_app/delete-account.html')
forms.py
class DeckForm(forms.ModelForm):
class Meta:
model = Deck
fields = {'deck', 'is_cedh'}
---
# All below was from the class and works
class CreateUserForm(UserCreationForm):
class Meta:
model = User
fields = {'username', 'email', 'password1', 'password2', }
class LoginForm(AuthenticationForm):
username = forms.CharField(widget=TextInput())
password = forms.CharField(widget=PasswordInput())
class UpdateUserForm(forms.ModelForm):
password = None
class Meta:
model = User
fields = {'username', 'email'}
exclude = {'password1', 'password2'}
# Updating profile picture
class UpdateProfilePicForm(forms.ModelForm):
profile_pic = forms.ImageField(widget=forms.FileInput(attrs={'class': 'form-control-file'}))
class Meta:
model = Profile
fields = {'profile_pic'}
models.py
class Deck(models.Model):
deck = models.CharField(max_length=150)
is_cedh = models.BooleanField(default=False)
user = models.ForeignKey(User,
max_length=10,
on_delete=models.CASCADE,
null=True)
def __str__(self):
return self.deck
---
# All below was from the class and works
class Profile(models.Model):
profile_pic = models.ImageField(null=True,
blank=True,
default='images/Default.png',
upload_to='images/')
user = models.ForeignKey(User,
max_length=10,
on_delete=models.CASCADE,
null=True)
def __str__(self):
return self.user
my-decks.html
<div class="container bg-white shadow-md p-5 form-layout">
{% if deck_name.count == 0 %}
<h3 class="">Looks like you don't have any decks. Add one below</h3>
{% else %}
<h3 class="">Your Decks</h3>
{% endif %}
{% for foo in deck_list %}
<p class="">{{ foo }}</p>
{% endfor %}
<br>
<br>
<h3 class="">Add New Deck</h3>
<form action=""
method="POST"
autocomplete="off">
{% csrf_token %}
{{ deck.is_cedh }}
{{ deck.deck }}
<br>
<input class="btn btn-primary"
type="submit"
name=""
value="Submit">
<br>
</form>
</div>
答: 暂无答案
评论