我正在django 2和python 3中构建一个预订系统,您可以在其中租用工具。用户使用名字和姓氏以及电子邮件进行注册。我扩展了django用户模型,并创建了一个名为“ profile”的模型,该模型具有以下字段:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
birthday = models.DateField(blank=True, null=True)
matrnr = models.CharField(max_length=6, validators=[MinLengthValidator(6), MaxLengthValidator(6)], default='', blank=True, null=True)
address = models.CharField(max_length=100, default='', blank=True, null=True)
city = models.CharField(max_length=50, default='', blank=True, null=True)
zip = models.CharField(max_length=6, validators=[MinLengthValidator(5), MaxLengthValidator(5)], default='', blank=True, null=True)
homephone = models.CharField(max_length=15, blank=True, default='', null=True)
mobilephone = models.CharField(max_length=15, blank=True, default='', null=True)
id_number = models.CharField(max_length=20, blank=True, default='', null=True)
driverlicense_number = models.CharField(max_length=20, blank=True, default='', null=True)
有一个包含可借用工具的页面,每个项目旁边都有一个“保留”按钮。如果用户现在单击该按钮,则应首先检查用户是否已填写其个人资料数据。如果没有,则用户必须先填写它们,然后才能借用一些东西。
我该如何实现?我是个笨手笨脚的人。
这是我列出所有工具的表格:
{% block title %}Tool List{% endblock %}
{% block content %}
<p> Here you see all the borrowable tools </p>
{% for instance in toollist %}
<table border="3" cellspacing="5">
<colgroup>
<col width="600">
<col width="100">
<col width="180">
</colgroup>
<tr>
<td><b>{{ instance.tool_name }}</b></td>
<td>{{ instance.tool_status }}</td>
{% if instance.tool_status == 1 %}
<td rowspan="2">
{% if user.is_authenticated %}
<a href="{% url 'reservation' %}">Reserve now</a>
{% else %}
<a href="{% url 'login' %}">Login for reservation</a>
</td>
{% endif %}
{% elif instance.tool_status == 0 %}
<td rowspan="2"> Not available </td>
{% endif %}
</tr>
<tr>
<td colspan="2">{{ instance.tool_text }}</td>
</tr>
</table>
<br>
这是我的注册视图:
def signup(request):
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
subject = 'Bestätige deine Registrierung beim Ausleihsystem'
message = render_to_string('registration/account_activation_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'token': account_activation_token.make_token(user),
})
from_email = settings.EMAIL_HOST_USER
email = form.cleaned_data.get('email')
recipient_list = [email]
send_mail(subject, message, from_email, recipient_list, fail_silently=False)
return redirect('account_activation_sent')
else:
form = SignUpForm()
return render(request, 'registration/signup.html', {'form': form})
def account_activation_sent(request):
return render(request, 'registration/account_activation_sent.html')
def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = User.objects.get(pk=uid)
except (TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.save()
login(request, user)
return redirect('home')
else:
return render(request, 'registration/account_activation_invalid.html')
def profile(request):
args = {'user': request.user}
return render(request, 'registration/profile.html', args)
这是edit_profile视图:
@login_required
@transaction.atomic
def edit_profile(request):
if request.method == 'POST':
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if profile_form.is_valid():
profile_form.save()
messages.success(request, _('Your profile was successfully updated!'))
return redirect('profile')
else:
messages.error(request, _('Please correct the error below.'))
else:
profile_form = ProfileForm(instance=request.user.profile)
return render(request, 'registration/edit_profile.html', {
'profile_form': profile_form
})