Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Request Method: POST
- Request URL: http://jqlc3gdp.apps.lair.io/login/set_alarm/
- Django Version: 2.0
- Exception Type: AttributeError
- Exception Value:
- 'WSGIRequest' object has no attribute 'username'
- Exception Location: /mnt/project/weather_alarm/views.py in get_user_timezone, line 94
- class User(models.Model):
- """ Model representing each User """
- username = models.CharField(max_length=20, unique=True)
- password = models.CharField(max_length=30, validators=[MinLengthValidator(8)])
- email = models.EmailField(max_length=254)
- first_name = models.CharField(max_length=20)
- last_name = models.CharField(max_length=20)
- class Alarm(models.Model):
- """ Model representing each Alarm """
- alarm_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
- username = models.ForeignKey(User, on_delete=models.CASCADE, null=True) # null=True is TEMP
- timezone = models.CharField(max_length=30)
- city = models.CharField(max_length=30)
- country = models.CharField(max_length=30)
- time = models.DateTimeField()
- temp_conditional = models.BooleanField()
- surf_conditional = models.BooleanField()
- temp_max = models.FloatField(blank=True, null=True)
- temp_min = models.FloatField(blank=True, null=True)
- surf_max = models.FloatField(blank=True, null=True)
- surf_min = models.FloatField(blank=True, null=True)
- class SetAlarmForm(ModelForm):
- ...
- class Meta:
- model = Alarm
- exclude = ['username', 'timezone', 'city', 'country']
- class AlarmCreateView(LoginRequiredMixin, CreateView):
- model = Alarm
- form_class = SetAlarmForm
- template_name = 'weather_alarm/set_alarm.html'
- success_url = reverse_lazy('weather_alarm:confirm-alarm')
- login_url = "/login/"
- def form_valid(self, form):
- self.get_user_location(form)
- return super().form_valid(form)
- def get_user_location(self, form):
- """ Function to get User's location from IP and create Alarm object """
- ...
- alarm_object = Alarm.objects.create(
- alarm_id=uuid.uuid4(),
- username=User.objects.get(username=self.request.username),
- timezone=user_timezone,
- city=location.raw['address']['city'],
- country=location.raw['address']['country'],
- time=form.cleaned_data['time'].astimezone(pytz.timezone(user_timezone)),
- temp_conditional=form.cleaned_data['temp_conditional'],
- surf_conditional=form.cleaned_data['surf_conditional'],
- temp_max=form.cleaned_data['temp_max'],
- temp_min=form.cleaned_data['temp_min'],
- surf_max=form.cleaned_data['surf_max'],
- surf_min=form.cleaned_data['surf_min'],
- )
- alarm_object.save()
Add Comment
Please, Sign In to add comment