【问题标题】:Editing an object with HTML form instead of Django model form使用 HTML 表单而不是 Django 模型表单编辑对象
【发布时间】:2019-02-06 18:43:41
【问题描述】:

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

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

    <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)')

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

@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

【问题讨论】:

    标签: django python-3.x django-models django-forms django-views


    【解决方案1】:

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

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

    urls.py

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

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

    @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 表单中添加什么 action 如果没有视图,就没有要添加的 url
    • 但您确实有意见...?如果您需要将值发送到服务器,则需要一个 url 和一个处理它的视图。您可以使用 post 或 get 或 ajax 提交表单,但它需要以一种或另一种方式到达服务器(唯一的方式是通过 url)。
    • 我知道这就是为什么我一直试图弄清楚如何为此制作视图。一旦我得到了看法。制作网址很容易url(r'^location/(?P&lt;username&gt;[-\w]+)/$', views.edit_location, name='edit_location'),
    • 我已经用 url/view/form 更新了我的答案,它们一起工作并且在不使用 Django 表单的情况下进行 POST 提交。如果您想在不刷新页面的情况下执行此操作,则需要从 jquery 进行 ajax 调用并从您的视图返回 json 响应。
    • 你认为我需要在 url 中传递用户参数吗?我现在正在尝试这个
    猜你喜欢
    • 2020-04-18
    • 2016-10-15
    • 2016-02-24
    • 2010-12-29
    • 1970-01-01
    • 2011-08-26
    • 2015-02-24
    • 2020-02-12
    • 1970-01-01
    相关资源
    最近更新 更多