SyntaxError:Python Django中的语法无效

时间:2016-12-21 20:41:39

标签: python html django web web-applications

我目前正在使用Python 3.4和Django 1.10.3创建一个基本上是“在线笔记本”的网站,用户可以在其中存储他们的笔记(条目和相应的主题)。

我项目的主文件夹目录位于 C:\用户\史蒂芬\ mynotes

我的应用程序的文件夹目录位于 C:\用户\史蒂芬\ mynotess (使用 double s - >我为可怕的命名道歉。我是网络应用程序开发的新手,我现在不想在更改名称时遇到麻烦)

因此,当我运行服务器时,在命令提示符中出现了此错误。

`Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0465FB70>
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "C:\Python34\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Python34\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "C:\Python34\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "C:\Python34\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python34\lib\site-packages\django\urls\resolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Python34\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python34\lib\site-packages\django\urls\resolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1467, in exec_module
  File "<frozen importlib._bootstrap>", line 1572, in get_code
  File "<frozen importlib._bootstrap>", line 1532, in source_to_code
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "C:\Users\Steven\mynotes\urls.py", line 23
    ]
    ^
SyntaxError: invalid syntax`

这是在 mynotes / urls.py (主项目文件夹)

from django.conf.urls import include, url
from django.contrib import admin


urlpatterns = [
    url(r'^admin/', include(admin.site.urls),
    url(r'', include('mynotess.urls', namespace= "mynotess")),
]

这是在 settings.py

ROOT_URLCONF = 'mynotes.urls'

这是在 mynotess / urls.py (apps文件夹)

from django.conf.urls import url
from . import views

urlpatterns = [
    #home page
    url(r'^$', views.index, name = 'index'),
    #topics page
    url(r'^topics/$', views.topics, name = 'topics'),
]

这是在 views.py 中。我在models.py中有一个Topic类和一个Entry类。 Topic类具有dateAdded属性(时间戳)

from django.shortcuts import render
from .models import Topic

def index(request):
    #home page
    return render(request, 'mynotess/index.html') #sends request to index.html template

def topics(request):
    #topics page
    topics = Topic.objects.order_by ('dateAdded')
    context = {'topics' : topics}
    return render(request, 'mynotess/topics.html', context)

mynotess / templates / mynotess / 里面我有三个html文件如下:

base.html (父模板)

<p> 
    <a href = "{% url 'mynotess:index' %}">My Notes</a> -
    <a href = "{% url 'mynotess:topics' %}">Topics</a>
</p>

{% block content %}{% endblock content %} 

index.html (主页)

{% extends "mynotess/base.html" %}

{% block content %}
<p>My Notes allows you to take notes online without having to worry about them getting deleted when your computer crashes! Type in anything you want to remember,
including lecture notes, passwords, ANYTHING!</p>
{% endblock content %}

topics.html (主题页面)

{% extends "mynotess/base.html" %}

{% block content %}

<p>Topics</p>

<ul>
{% for topic in topics %}
<li>{{topic}}</li>
{% empty %}
<li>No topics to show.</li>
{% endfor %}
</ul>

{% endblock content %}

localhost:8000不会因此而加载。我真的不知道我在哪里出错了。我认为错误与URL有关,但我不知道它到底有什么问题。任何形式的帮助都将受到高度赞赏!谢谢!

1 个答案:

答案 0 :(得分:0)

在您的网址中,它应为url(r'^admin/', include(admin.site.urls)),