简体   繁体   English

如何在模型Django中创建两个视图函数

[英]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.

相关问题 如何将视图中的变量值存储到 Django 模型? - How can I store the variables values from views to the Django models? 在 Django 中,我如何根据两个模型中的字段自动创建多对多条目 - In Django how I can create automatically a many to many entry based on a field found in two models 我可以像处理模型或视图一样从文件夹中的文件中导入 function 吗? - Can I import a function from a file in a folder with Django as I would do with the models or views? 如何在 Django 中检查两个模型是否相等? - How can I check if two models equal each other in Django? Django:如何显示两个或三个模型的数据 - Django: how can i show the data of two or three models Django REST-如何使用两个模型获取JSON? - Django REST - How can I get a JSON with two models? 如何过滤两个模型之间的差异? 使用 django - how can I filter the difference between the two models? using django 如何从两个视图Django传递相同的上下文? - How can I pass the same context from two views, Django? django 模型 - 我如何创建抽象方法 - django models - how can i create abstract methods 如何将我在视图中获得的一些信息保存到模型中的某个字段 - django - how can i save some information that i got in views to one of fields in models - django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM