使用HTML表单而不是Django模型表单编辑对象

时间:2018-08-31 17:42:02

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

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

简介:我在Index.html的右栏中有一个非常简单的html(不可见)表单。当用户单击public static void main(String[] args) throws UnknownHostException, AlienReaderException, InterruptedException { //SpringApplication.run(VolvoRfidReaderApplication.class, args); ConfigurableApplicationContext run = SpringApplication.run(VolvoRfidReaderApplication.class, args); VolvoRfidReaderApplication app = run.getBean(VolvoRfidReaderApplication.class); app.run(); } private void run(){ Runnable r1= () -> { for(int i=0;i<30;++i){ //System.out.println("task 1"); try { new AlienReader("222.22.222.22", 22, "username","password"); } catch (UnknownHostException | AlienReaderException | InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ Thread.sleep(1000); }catch(Exception ignored){ } } }; Runnable r2= () -> { for(int i=0;i<30;++i){ //System.out.println("task 2"); try { new AlienReader("333.33.333.33", 22, "username","password"); } catch (UnknownHostException | AlienReaderException | InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ Thread.sleep(1000); }catch(Exception ignored){ } } }; ExecutorService service = Executors.newFixedThreadPool(2); service.submit(r1); service.submit(r2); service.shutdown(); } 按钮时。表单会自动填写用户的纬度和经度详细信息,然后单击提交(用户看不到表单)。我使用jQuery来实现

Locate me

enter image description here

我的模型是

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

我也以HTML格式@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") 使用为此视图创建的URL

1 个答案:

答案 0 :(得分:2)

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

您可以创建一个表单类,该类与您在接收数据(但不使用它显示表单)时向验证等手动编写的类相对应。如果这样做,您可能需要在HTML表单中添加{% csrf_token %}(或将视图标记为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>