я модель под названием UserProfile определяется следующим образом:Django модели атрибутов правильно спасены формами, но возвращает тип None при запросе
class UserProfile(models.Model):
user = models.OneToOneField(User)
website = models.URLField(blank=True, null=True)
location = models.CharField(max_length=200, null=True)
longitude = models.FloatField(null=True)
latitude = models.FloatField(null=True)
credit = models.FloatField(default=0, null=True)
def __unicode__(self):
return self.user.username
«регистрация» страница включает в себя следующие формах:
Когда его представляется она идет:
def register(request):
registered = False
if request.method == "POST":
user_form = UserForm(request.POST)
profile_form = UserProfileForm(request.POST)
if user_form.is_valid() and profile_form.is_valid():
print(request.POST)
lat = request.POST.get('lat')
print("lat is: " + lat)
lng = request.POST.get('lng')
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.latitude = lat
profile.longitude = lng
profile.save()
else:
print user_form.errors, profile_form.errors
else:
profile_form = UserProfileForm()
user_form = UserForm()
return render(request, "register.html", {'user_form' : user_form, 'profile_form' : profile_form})
это все, кажется, работает хорошо, но когда я пытаюсь запросить эти данные в другой точке зрения, местоположение, широта, и LNG свойства придумать как типы «None».
def link(request):
print("trying to authenticate...")
if request.user.is_authenticated():
user_profile = UserProfile(user=request.user)
else:
return render(request, 'login.html')
if request.method == "POST":
print("request was a post.")
if request.is_ajax():
print("request is ajax.")
print("User profile is: " + str(user_profile))
# returns billy, so we're looking at the right object
print("user location is: " + str(user_profile.location))
user_longitude = user_profile.longitude
user_latitude = user_profile.latitude
distance = request.POST.get('distance')
value = request.POST.get('value')
Как вы можете видеть, данные, поступающие сюда отличается от того, что возвращается запросов оболочки:
User profile is billy
user location is: None
user longitude is: None
Я очень смущен относительно того, как местоположение, и т.д. типы с плавающей точкой в представление оболочки и регистра, но нет типов в представлении «ссылка». Есть идеи?
Как вы получаете user_profile в методе POST – Windsooon
@Aison user_profile является взаимно однозначным, связанным с объектом пользователя, возвращаемым request.user –