【问题标题】:Views.py not recognised by server. Server won't recognise code or raise exceptions for bad code in file服务器无法识别 Views.py。服务器不会识别代码或引发文件中错误代码的异常
【发布时间】:2018-08-04 12:08:42
【问题描述】:

我正在努力展示我通过管理 GUI 创建的一些模型对象。 我无法通过我的查看功能让它们显示在网页上。

(edit:)在进行了一些挖掘之后,我意识到当我破坏views.py 文件时服务器不会引发异常(所有其他文件都可以正常工作)。为什么服务器不读取视图文件?

所有文件都位于 /jobboard。 Jobboard 列在设置文件的 INSTALLED_APPS 中,jobboard 中的所有其他文件都可以正常工作。

我还注意到我的项目文件夹中没有 admin.py。这正常吗?

Views.py:

from django.shortcuts import render
from django.views.generic import TemplateView
from django.contrib.auth.decorators import staff_member_required
from .models import JobPost
from django.utils import timezone


def jobs(request):   
    JobPost.objects.filter(published_date__lte=timezone.now()).order_by('published_date')

    latest_post_list = JobPost.objects.order_by('-pub_date')

    context = {

        'deadline': deadline,
        'created_at': created_at, 
        'wordcount':wordcount, 
        'jobtaken':jobtaken,
        'JobPost':JobPost,
        'latest_post_list':latest_post_list,
    }

    return render(request, 'jobboard/jobs.html', context=context)

URL.py:

from django.contrib import admin
from django.urls import path, re_path, include
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from login import views

urlpatterns = [

    path('admin/', admin.site.urls),
    #re_path(r'^login/$', auth_views.login(template_name = 'accounts/login.html'), name='login'),
    re_path(r'^signup/$', views.signup, name='signup'),
    path('login/', include('login.urls')),
    path('', views.index, name='index'),
    path('accounts/', include('django.contrib.auth.urls')),
    path('jobs/', views.jobs, name='jobs'),
    path('users/', include('users.urls')),
    path('users/', include('django.contrib.auth.urls')),
]

目录结构:

gpproject
│   ├── db.sqlite3
│   ├── gp.sublime-project
│   ├── gp.sublime-workspace
│   ├── grad
│   │   ├── __init__.py
│   │   ├── Procfile1
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-35.pyc
│   │   │   ├── settings.cpython-35.pyc
│   │   │   ├── urls.cpython-35.pyc
│   │   │   └── wsgi.cpython-35.pyc
│   │   ├── settings.py
│   │   ├── static
│   │   │   ├── office.jpeg
│   │   │   └── pics
│   │   │       └── office.jpeg
│   │   ├── urls.py
│   │   ├── views.py
│   │   └── wsgi.py
│   ├── jobboard
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── 0002_auto_20180716_1122.py
│   │   │   └── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   └── views.py
│   ├── login
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │       ├── 0001_initial.cpython-35.pyc
│   │   │       └── __init__.cpython-35.pyc
│   │   ├── models.py
│   │   ├── __pycache__
│   │   │   ├── admin.cpython-35.pyc
│   │   │   ├── apps.cpython-35.pyc
│   │   │   ├── __init__.cpython-35.pyc
│   │   │   ├── models.cpython-35.pyc
│   │   │   ├── urls.cpython-35.pyc
│   │   │   └── views.cpython-35.pyc
│   │   ├── static
│   │   │   └── pics
│   │   │       └── office.jpeg
│   │   ├── templates
│   │   │   ├── login
│   │   │   │   └── office.jpeg
│   │   │   ├── loginbutton.html
│   │   │   ├── logoutbutton.html
│   │   │   ├── navbar.html
│   │   │   ├── registration
│   │   │   │   ├── login.html
│   │   │   │   ├── logintut.html
│   │   │   │   ├── password_reset_complete.html
│   │   │   │   ├── password_reset_confirm.html
│   │   │   │   ├── password_reset_done.html
│   │   │   │   ├── password_reset_email.html
│   │   │   │   └── password_reset_form.html
│   │   │   └── signup.html
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── manage.py
│   ├── oldbackupdb.sqlite3
│   ├── templates
│   │   ├── base.html
│   │   ├── footer.html
│   │   ├── Header.html
│   │   ├── index2.html
│   │   ├── index.html
│   │   ├── jobpost.html
│   │   ├── jobs.html
│   │   ├── navbar.html
│   │   ├── oldHeader.html
│   │   ├── sidenav.html
│   │   ├── signup.html
│   │   └── userdropdown.html
│   ├── universities.xcf
│   └── users
│       ├── admin.py
│       ├── apps.py
│       ├── forms.py
│       ├── __init__.py
│       ├── migrations
│       │   ├── 0001_initial.py
│       │   ├── __init__.py
│       │   └── __pycache__
│       │       ├── 0001_initial.cpython-35.pyc
│       │       └── __init__.cpython-35.pyc
│       ├── models.py
│       ├── __pycache__
│       │   ├── admin.cpython-35.pyc
│       │   ├── forms.cpython-35.pyc
│       │   ├── __init__.cpython-35.pyc
│       │   ├── models.cpython-35.pyc
│       │   ├── urls.cpython-35.pyc
│       │   └── views.cpython-35.pyc
│       ├── tests.py
│       ├── urls.py
│       └── views.py
├── gradproofworkspace.code-workspace
├── index1.html
├── office.jpeg
├── Pipfile
├── Pipfile.lock
├── Procfile
├── README.md
├── requirements.txt
└── runtime.txt

感谢您的帮助

【问题讨论】:

  • jobs 函数中存在语法错误。 context: { 应该是 context = {。请确保问题中的代码是您正在运行的实际代码,并且语法是有效的 python。 minimal reproducible example
  • 此外,您的 JobPost 模型没有 pub_date 字段或 published_date 字段,因此此代码也会引发异常。
  • 有趣。当我运行服务器时,它显示“系统未发现问题 (0)”。
  • jobs 视图函数会在运行时引发异常,我认为在导入时也是如此,因为语法错误(除非 python 将其解释为类型提示?)。您确定您没有使用来自其他文件的视图吗?
  • 不仅有语法错误,还有几个名称错误。你根本不会跑。您没有看到错误的事实表明此代码甚至没有被导入,更不用说运行了。请发布您的目录结构,准确显示此视图文件的位置,以及您的 URLs.py。

标签: django django-views


【解决方案1】:

您没有导入作业视图。在您的 urls.py 中,名称 views 仅从 login 应用程序导入。

from login import views
...
path('jobs/', views.jobs, name='jobs'),

你可以这样改:

from jobboard.views import jobs
...
path('jobs/', jobs, name='jobs'),

【讨论】:

  • 为什么您的原始路由实际上有效仍然是一个谜,因为大概没有login.views.jobs 视图,因此您在访问/jobs/ 时会遇到异常。
【解决方案2】:

感谢 Håken,它为我指明了正确的方向。虽然更准确地说我只是错过了这行代码:

from jobboard import views

【讨论】:

    猜你喜欢
    • 2015-11-24
    • 2015-12-12
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多