如何制作Django应用程序来追踪人们的体重

时间:2018-06-15 15:21:03

标签: python django

我正在Django做一个应用程序来上传人们的体重并允许他们每天跟踪他们的进化。

我有以下模型,表单,视图和网址:

Models.py

class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
heigth = models.IntegerField('Altura', blank=True, null=True)
weigth = models.DecimalField('Peso', max_digits=5, decimal_places=2, blank=True, null=True)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
    Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()

views.py

@login_required
@transaction.atomic
def update_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(settings.LOGIN_REDIRECT_URL)
    else:
        messages.error(request, _('Please correct the error below.'))
else:
    profile_form = ProfileForm(instance=request.user.profile)
return render(request, 'catalog/profile.html', {
    'profile_form': profile_form
})

Forms.py

class ProfileForm(forms.ModelForm):
class Meta:
    model = Profile
    fields = [
        "heigth",
        "weigth",
def __init__(self, *args, **kargs):
    super(ProfileForm, self).__init__(*args, **kargs)

urls.py

urlpatterns = [
url(r'^profiles/update/$', views.update_profile, name='update_profile'),]

我希望在每个用户的模型中监控WEIGHT字段值的更新,并生成一个图表,其中包含每个用户当天的日期和上次保存值。就像WEIGHT TRACKER.

一样

有没有人知道怎么做?

0 个答案:

没有答案