У меня есть модель профиля, в которой слишком много полей. Два поля lat
и lon
Я использую форму django для редактирования всех полей ниже
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
city = models.CharField(max_length=100)
state = models.CharField(max_length=100, blank=True, null=True)
country = models.CharField(max_length=100)
street_address = models.CharField(max_length=500, null=True, blank=True)
zip_code = models.CharField(max_length=10, default='')
phone_number = models.CharField(max_length=17, blank=True, null=True)
age = models.DateField(blank=True, null=True)
member_since = models.DateTimeField(auto_now_add=True)
profile_image = models.ImageField(default='', blank=True, null=True)
bio = models.TextField(default='', blank=True)
bio_images = models.ImageField(default='', blank=True, null=True)
activities_i_do = models.TextField(default='', blank=True, null=True)
activities_i_love = models.TextField(default='', blank=True, null=True)
is_verified = models.BooleanField(default=False)
lat = models.FloatField(blank=True, null=True)
lon = models.FloatField(blank=True, null=True)
Ниже приведен вид редактирования атрибутов
@login_required
def edit_profile(request):
if request.method == 'POST':
user_form = UserEditForm(data=request.POST or None, instance=request.user)
profile_form = ProfileEditForm(data=request.POST or None, instance=request.user.profile, files=request.FILES)
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
return redirect('accounts:profile', username=request.user.username)
else:
user_form = UserEditForm(instance=request.user)
profile_form = ProfileEditForm(instance=request.user.profile)
context = {'user_form': user_form,
'profile_form': profile_form}
return render(request, 'accounts/profile_edit.html', context)
Теперь вопрос в том, можно ли использовать другую форму, чтобы просто отредактировать поля lat
lon
в приведенном выше примере модели, как показано ниже
<form method="post" action="{{ ??????????? }}">
<input id="jsLat" type="text" placeholder="latittude" >
<input id="jsLon" type="text" placeholder="longitude">
<button type="submit" id="submit">Submit</button>
</form>
![enter image description here](https://i.stack.imgur.com/lE8QI.png)
Я сделал форму с помощью jquery, чтобы при щелчке пользователя находить меня форма автоматически заполнялась и отправлялась сама. Можно ли использовать эту форму для редактирования всего 2 полей модели профиля
ниже находится форма для редактирования моего профиля
class ProfileEditForm(forms.ModelForm): #UserProfileForm or ProfileEdit
class Meta:
model = Profile
fields = ('city', 'state', 'country', 'street_address', 'zip_code', 'phone_number', 'age', 'profile_image',
'bio', 'bio_images', 'activities_i_do', 'activities_i_love', 'lat', 'lon')
PS: Код геоджанго удален, чтобы сделать его простым. Просто нужно обновить lat
lon
, как если бы они были обычными полями