为什么我的观点一直出现404错误?

时间:2012-10-10 10:45:31

标签: python django forms django-views

我在Ubuntu 12.04上使用Django 1.4和Python 2.7。

之前我写了很多观点,但是我有点困惑的是为什么我在这个上遇到404错误。

urls.py:

url(r'^addNewClient/', 'gateway_interface.views.add_new_client'),

views.py

def add_new_client(request):
    """
    ..  function:: add_new_client()

        Provide a form for entering new client information

        :param request: Django Request object
    """
    ## Create a logging object
    path = os.path.join(os.path.dirname(__file__), 'logs/')
    filename = '{0}addNewClient.log'.format(path)
    logfile = open(filename, 'a')
    now = datetime.datetime.now()
    logfile.write('\n --------------------- {0}\n'.format(now))

    try:
        form = addClientInfo()
    except:
        output = "Handle Error: Cannot create a valid form"
        logfile.write('{0}\n'.format(output))
        logfile.write('{0}\n\n'.format(traceback.format_exc()))
        logfile.close()
        return HttpResponse(output)

    try:
        data = {}
        data.update(csrf(request))
        data.update({ 'form' : form })
    except:
        output = "Handle Error: Cannot generate CSRF token"
        logfile.write('{0}\n'.format(output))
        logfile.write('{0}\n\n'.format(traceback.format_exc()))
        logfile.close()
        return HttpResponse(output)

    return render_to_response("addNewClientInfo.html", data)

forms.py

class addClientInfo(forms.Form):
    """
    ..  class:: addClientInfo

        Create a form to enter new client info
    """
    my_gateway_choices = tuple(gateway_proper_names.items())

    # Create the post fields
    tkz_client_name = forms.CharField(max_length = 50, required = True)
    tkz_gateway = forms.ChoiceField(widget = forms.Select(), choices = my_gateway_choices, required = True)
    tkz_gateway_api_id = forms.CharField(max_length = 50, required = True)
    tkz_gateway_trans_key = forms.CharField(max_length = 50, required = True)
    tkz_ys_api_id = forms.CharField(max_length = 50, required = True)
    tkz_ys_trans_key = forms.CharField(max_length = 50, required = True)

有什么想法吗?

EDIT1:

我应该提到我在错误日志中没有收到任何信息。 .html文件与我的所有其他模板一起存在......在同一个地方。

2 个答案:

答案 0 :(得分:1)

通常网址模式必须非常精确。尝试对视图使用以下url模式:

url(r'^addNewClient/$', 'gateway_interface.views.add_new_client'),

该模式确保只使用addnewClinet/匹配$。否则,url模式只匹配字符串的开头,而不是整个字符串,因此addNewClient/foo之类的内容也会匹配。

答案 1 :(得分:-1)

当我将DEBUG=True抛出异常时,我能够确定我没有导入我正在尝试使用的表单:addClientInfo()

非常新鲜的我。困难在于我收到的错误似乎并未将此视为问题。希望其他人可以看到误导性错误可能存在根本不明显的潜在问题。

最好,全部!