[英]How can I create two views function in a models Django
I am making a food website. 我正在制作食品网站。
When users upload a review about a restaurant, 当用户上传有关餐厅的评论时,
I want to save the restaurant name the users typed and the position according to the Google Map API. 我想根据Google Map API保存用户键入的餐厅名称和位置。
The information all save in the Restaurant model 信息全部保存在餐厅模型中
class Restaurant(models.Model):
restaurant_id = models.CharField(max_length=20, primary_key=True)
name = models.CharField(max_length=200)
longitude = models.CharField(max_length=200)
latitude = models.CharField(max_length=200)
def __str__(self):
return self.name.encode('utf-8', errors='replace')
I have two views function to save the data. 我有两个视图功能来保存数据。
I use Ajax to get the position's data 我使用Ajax获取职位数据
def position(request):
if request.method == 'GET':
local_lat = request.GET['latitude']
local_lng = request.GET['longitude']
Restaurant.objects.create(
latitude = local_lat,
longitude =local_lng,
)
return HttpResponse(lat)
And use create views functions to get the restaurant's name which users posted 并使用创建视图功能获取用户发布的餐厅名称
def create(request):
if request.method == 'POST':
local_form = AddForm(request.POST,request.FILES)
if local_form.is_valid():
local_restaurant = local_form.data.get("restaurant")
local_restaurant_id = str(len(Restaurant.objects.all()) + 1)
Restaurant.objects.create(
restaurant_id = local_restaurant_id,
name = local_restaurant,
)
return render(request, 'index.html')
else:
local_form = AddForm(request.POST)
return render(request, 'create_meal.html',)
else:
return render(request, 'create_meal.html',)
However, I can't really save the data into database because I use two views function to create the same model. 但是,我不能真正将数据保存到数据库中,因为我使用两个视图函数来创建相同的模型。
How can I solve it? 我该如何解决?
=================Added================= ================添加================
In my html file, I have the form to that the user type their restaurant 在我的html文件中,我具有用户输入其餐厅的表格
<form id="add-meal-form" class="form" action="#" method="POST" role="form" >
{% csrf_token %}
{{ form.as_p }}
<div class="form-group">
<input type="text" class="form-control " id="restaurant" name="restaurant" >
</div>
<div class="form-group">
<input type="text" class="form-control " id="meal" name="meal" >
</div>
<div class="text-center">
<button type="submit" id="send" class="btn btn-primary" href="/">Submit</button>
</div>
</form>
If the user type the restaurant name, it will obtain it's latitude and longitude at the same time. 如果用户输入餐厅名称,它将同时获得其经度和纬度。
If the user submit the button, the restaurant name will be saved in Restaurant models. 如果用户提交按钮,则餐厅名称将保存在餐厅模型中。
I don't understand how to save these two data at the same time. 我不明白如何同时保存这两个数据。
If I understand you right you can update an instance in the database instead of creating one more. 如果我理解正确,则可以更新数据库中的一个实例,而无需再创建一个。 All you need is to access the instance with the pk: 您所需要做的就是使用pk访问实例:
restaurant = Restaurant.objects.get(restaurant_id=some_id)
then update it: 然后更新它:
restaurant.latitude = local_lat
restaurant.longitude = local_lng
and then save it: 然后保存:
restaurant.save()
All you have to think about now is how to pass some_id
to the update view. 您现在只需考虑的是如何将some_id
传递到更新视图。 If you use Ajax you probably can do this with HTML data
attribute and then get it with javascript and pass it with your ajax request. 如果您使用Ajax,则可能可以使用HTML data
属性执行此操作,然后使用javascript获取并通过ajax请求将其传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.