0

我有以下代码,你可以看到C_account()视图中有一个函数,但我仍然得到

**Exception Type: ViewDoesNotExist at /create_account/

Exception Value: Could not import EPM.views.C_account. View does not exist in module EPM.views.**

任何想法可能是什么问题?

EPM/views.py包含C_account函数定义的视图 ( ):

from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext
from EPM.forms import *
from EPM.models import *
from datetime import date
from django.contrib.sessions.models import Session
from django.conf.urls.defaults import *

from django.forms.formsets import formset_factory

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'deducive.settings'

from django.core.management import setup_environ
from deducive import settings
import csv


def C_account(request):
 if request.method == 'POST':
    form = CreateAccountForm(request.POST)
    if form.is_valid():

    acc_act_date = form.cleaned_data['Account_Activation_Date']
    present_date = date.today()
    if acc_act_date <= present_date:
        stat = 'active'
    else:
        stat = 'inactive'

    acc_cat_id = Account_Categories_T.objects.get(cat_name = stat, category = 'status')


        sto =   GL_Account_T(account_number=form.cleaned_data['Account_Number'],
                account_name=form.cleaned_data['Account_Name'],
                                account_description=form.cleaned_data['Account_Description'],
                                parent_account_number=form.cleaned_data['Parent_Account_Number'],
                                account_manager=form.cleaned_data['Account_Manager'],
                                account_category_id = acc_cat_id,
                                account_activation_date=form.cleaned_data['Account_Activation_Date'],
                                )
        sto.save()

        return HttpResponseRedirect('/create_account/thanks/')

else:
    form = CreateAccountForm()
return render_to_response('CreateAccountForm.html', {'form': form}, context_instance=RequestContext(request))

def thanks(request):
    return render_to_response('Thanks.html')

和 URLConf ( EPM/urls.py),它的C_account视图正确连接到create_account/URL:

from django.conf.urls import patterns, include, url

urlpatterns = patterns('EPM.views',
    (r'^create_account/$', 'C_account'),
    (r'^create_account/thanks/$', 'thanks'),)
4

2 回答 2

2

很高兴听到你解决了这个问题。

稍微令人困惑的ViewDoesNotExist异常可能是因为 DjangoImportError在读取时遇到了views.py(其中有from EPM.forms import *,由于您提到的字段拼写错误,它会触发异常);当前的行为是吞下许多异常(例如ImportError)并将它们重新引发为ViewDoesNotExist异常。

有一个四年前的Django 错误讨论如何使这些错误消息更有帮助(主要是通过捕获更少的异常)。同时,您可以通过尝试来追踪这些类型的问题,例如

from EPM.views import *

在 shell ( ./manage.py shell) 中,它将向您显示原始异常。

于 2012-07-10T16:12:05.793 回答
0

我终于破解了我的错误。问题出在我的 forms.py 中,我在一个地方将 CharField 写为 Charfield。但我仍然想知道为什么它指向我通过在 shell 中forms.py 导入我得到它的错误。views.py

于 2012-07-10T13:51:06.297 回答