简体   繁体   中英

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.

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

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

<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:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM