Django表单提交网址不工作 [英] Django form submit url not working

查看:126
本文介绍了Django表单提交网址不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手,不知道为什么我的表单不工作。似乎在表单提交后,即使网址被更改为 / 82nsj / update ,它仍然会转到索引方法。

I'm new to Django and cannot figure out why my form is not working. It seems that after form submit even though the url is being changed to /82nsj/update it is still going to the index method in the view.

视图

from business.models import Business, Token
from django.shortcuts import render
from business.forms import BusinessForm

def index(request, token):
    try:
        business = Business.objects.get(token__token=token)
    except Token.DoesNotExist:
        business = None
    except Business.DoesNotExist:
        business = None

    form = BusinessForm(instance=business)
    return render(request, 'business/index.html', {'form': form})

def update(request, token):
    try:
        business = Business.objects.get(token__token=token)
    except Token.DoesNotExist:
        business = None
    except Business.DoesNotExist:
        business = None

    form = BusinessForm(request.POST, instance=business)
    if form.is_valid():
        form.save()
        return render(request, 'business/index.html', {'form': form})

网址 p>

Urls

url(r'^$', 'business.views.index', name='home'),
url(r'^business/(?P<token>\w+)/', 'business.views.index', name='business'),
url(r'^business/(?P<token>\w+)/update/', 'business.views.update', name='business_update'),

表单

self.helper = FormHelper(self)
self.helper.form_class = 'form-horizontal'
self.helper.form_action = 'update/'
self.helper.form_method = 'post'
self.helper.layout = Layout(
    HTML("<p class='alert-info alert'>Please confirm your business contact information is updated and correct.</p>"),
    Div(
        'my',
                'fields',
        FormActions(
            Submit('save_changes', 'Save changes', css_class="btn-primary"),
        ),
        css_class='row-fluid'
    )
)

我知道这不工作,因为我的索引和我的更新中设置了断点,在我按提交按钮后,只有索引断点正在到达。

I know this isn't working because I have breakpoints setup in my index and in my update and after I press the submit button only the index breakpoint is being reached.

我正在做的错误是防止

推荐答案

这可能是一个问题与您的 url(... 正则表达式。

It's probably an issue with your url(... regex.

Django将转到匹配的第一个URL,如果你有这个

Django will go to the first URL that matches so if you have this

url(r'^business/(?P<token>\w+)/', 'business.views.index', name='business'),
url(r'^business/(?P<token>\w+)/update/', 'business.views.update', name='business_update'),

转到 / business /< token> / anything_goes_here 将永远转到 business.views.index

为了阻止这一点,请在 $ $ 表达式结尾中添加

To stop this, include a $ for end of expression.

url(r'^business/(?P<token>\w+)/$', 'business.views.index', name='business'),

现在您的 / business /< token> / update / 不匹配第一个URL,然后匹配 business.views.update

Now your /business/<token>/update/ wont match the first URL and will then match business.views.update.

这篇关于Django表单提交网址不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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