0

我正在制作一个基本的考勤卡程序。从 urls.py 导入视图时出现错误。可以导入应用程序中的其他文件(模型、表单等),但是任何对导入视图的调用都会返回我以前从未见过的错误。

Request Method: GET
Request URL: http://127.0.0.1:8000/admin/

Django Version: 1.5
Python Version: 3.3.0
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'timesheets')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "C:\Python33\lib\site-packages\django\core\handlers\base.py" in get_response
  103.                     resolver_match = resolver.resolve(request.path_info)
File "C:\Python33\lib\site-packages\django\core\urlresolvers.py" in resolve
  319.             for pattern in self.url_patterns:
File "C:\Python33\lib\site-packages\django\core\urlresolvers.py" in url_patterns
  347.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python33\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
  342.             self._urlconf_module = import_module(self.urlconf_name)
File "C:\Python33\lib\site-packages\django\utils\importlib.py" in import_module
  35.     __import__(name)
File "c:\timecards\timecards\urls.py" in <module>
  10.   url(r'^/', include('timesheets.urls')),
File "C:\Python33\lib\site-packages\django\conf\urls\__init__.py" in include
  25.         urlconf_module = import_module(urlconf_module)
File "C:\Python33\lib\site-packages\django\utils\importlib.py" in import_module
  35.     __import__(name)
File "c:\timecards\timesheets\urls.py" in <module>
  2. from timesheets import views

Exception Type: SystemError at /admin/
Exception Value: ..\Objects\tupleobject.c:143: bad argument to internal function

我的 timesheets.urls.py 是:

from django.conf.urls import patterns, include, url
from timesheets import views
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'timecards.views.home', name='home'),
    # url(r'^timecards/', include('timecards.foo.urls')),
    url(r'^$', views.login),
    url(r'^logout/$', views.logout),
    url(r'^login/$', views.login),
    url(r'^timecards/$', views.timecards), 
    url(r'^timecards/add/$', views.addcard),
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

timesheets.views.py 是:

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from timesheets.forms import AppointmentForm
from timesheets.models import Appointment

def login(request):
    context={'next':'/timecards/', 'username':''}
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        context['username'] = ''
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                redirect('timecards')
        else:
            context['error'] ='Did not find match for username and password'
    render('login.html', context)

def logout(request):
    logout(request)
    redirect('login')

def timecards(request):
    if not request.user.is_authenticated():
        redirect('login')
    employee = request.user
    timecards = Appointment.objects.get('employee_id'=employee.id)
    context = {'employee': employee, 'timecards':timecards}
    return render(request, 'timecards.html', context)

def addcard(request):
    if not request.user.is_authenticated():
        redirect('login')
    employee = request.user
    if request.method == 'POST':
        form = AppointmentForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            appoint = form.save(commit=False)
            appoint.employee_id = employee.id
            appoint.save()
            return redirect('timecards')
    else:
        form = AppointmentForm()
    context = {'employee': employee, 'form':form}
    return render(request,'addcards.html', context)
4

1 回答 1

1

有类似的问题,但我的应用程序有问题,上述解决方案均无效。我尝试在 python 控制台中运行我的 views.py 并在其中发现了错误。在我改变后它对我有用。

于 2013-08-17T10:25:35.513 回答