使用HTML表单而不是Django模型表单编辑对象 [英] Editing an object with HTML form instead of Django model form

查看:109
本文介绍了使用HTML表单而不是Django模型表单编辑对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HTML表单而不是Django模型表单来编辑对象。我需要知道如何对此进行查看(在下面以粗体显示我的不完整视图

I am trying to edit my objects using HTML form instead of Django model form. I need to know how to make the view for this (See my incomplete view below in bold)

简介:我在Index.html的右栏中有一个非常简单的html(不可见)表单。当用户点击找到我按钮时。该表格会自动填写用户的纬度和经度详细信息,然后单击提交(用户看不到表格)。我使用jQuery来实现

Intro: I have a very simple html (invisible )form in the right column of my Index.html. when the user clicks on Locate me button. The form automatically fills the users latitude and longitude details and clicks submit(the user does not see the form). I use jQuery to achieve this

    <form method="post" action="#">
        {% csrf_token %}
        <input id="jsLat" type="text" placeholder="latittude" name="LAT">
        <input id="jsLon" type="text" placeholder="longitude" name="LON">
        <button type="submit" id="submit">Submit</button>
    </form>

我的模型

class UserLocation(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    lat = models.FloatField(blank=True, null=True)
    lon = models.FloatField(blank=True, null=True)
    point = models.PointField(srid=4326, default='SRID=4326;POINT(0.0 0.0)')

我的不完整视图(我需要帮助才能完成此操作)

My incomplete view(I need help completing this)

@login_required
def edit_location(request):
    if request.method == 'POST':
      form = ??????????? #don't know how to call the HTML form   
        user = request.user              
        lat = request.POST.get('LAT') #lat is a field in the model see above
        lon = request.POST.get('LON') #lon is a field in the model see above
        form.save(commit=False) #Not sure if I can use this with HTML form
        point = lat (some operation) lon 
        form.save()
     return redirect("home") 

也是HTML形式的 action 我是否使用为此视图创建的URL

Also in the HTML form action do I use the URL created for this view

推荐答案

如果您从 request.POST 中手动选择值,则视图中不需要表单。只需直接创建或修改模型实例即可。

You don't need a form in your view if you're manually picking the values from request.POST. Just create or modify a model instance directly.

您可以创建一个表单类,该表单类与您向验证等手动编写的表单类相对应。(但不使用它来显示表单)。如果这样做,您可能需要在HTML表单中添加 {%csrf_token%} (或将视图标记为csrf豁免)。

You can create a form class that corresponds to the one you're writing manually to the the validation etc. when receiving data (but without using it to display the form). You'll likely need to add {% csrf_token %} to your html form if you do this (or mark the view csrf exempt).

urls.py

...
(r'^/my/special/url/', views.edit_location),

views.py(手动提取request.POST参数,以及更新模型):

views.py (manually pulling request.POST parameters, and updating a model):

@login_required
def edit_location(request):
    if request.method == 'POST':
        #location,created = UserLocation.objects.get_or_create(user=request.user)
        location = UserLocation.objects.get_or_create(user=request.user)[0]
        location.lat = request.POST.get('LAT') #lat is a field in the model see above
        location.lon = request.POST.get('LON') #lon is a field in the model see above
        location.point = lat (some operation) lon 
        location.save()
     return redirect("home") 

表格:

<form method="post" action="/my/special/url/">
    <input id="jsLat" type="text" placeholder="latittude" name="LAT">
    <input id="jsLon" type="text" placeholder="longitude" name="LON">
    <button type="submit" id="submit">Submit</button>
</form>

这篇关于使用HTML表单而不是Django模型表单编辑对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆