为什么我的服务器开始运行?

时间:2017-06-14 00:30:43

标签: python django

为什么在尝试运行我的服务器访问数据库以查看我的代码是否有效时会出现错误?在我Terminal的项目文件夹中,我运行sudo python manage.py runserver尝试运行服务器,但由于上述错误,它无法运行。我已经环顾四周但却无法找到与我的问题直接相关的内容。

我猜测我的if()声明就是问题。

我得到的错误是:

RuntimeError: maximum recursion depth exceeded while calling a Python object

这是我的views.py文件:

from .models import Album
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.core.urlresolvers import reverse_lazy
from django.views import generic
from django.views.generic import View
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .forms import UserForm

class IndexView(generic.ListView):
    template_name = 'music/index.html'
    context_object_name = 'all_albums'

    def get_queryset(self):
        return Album.objects.all()

class DetailView(generic.DeleteView):
    model = Album
    template_name = 'music/detail.html'

class AlbumCreate(CreateView):
    model = Album
    fields = ['artist', 'album_title', 'genre', 'album_logo']

class AlbumUpdate(UpdateView):
    model = Album
    fields = ['artist', 'album_title', 'genre', 'album_logo']

class AlbumDelete(DeleteView):
    model = Album
    success_url = reverse_lazy('music:index')

class UserFormView(View):
    form_class = UserForm
    template_name = 'music/registration_form.html'

    # display blank form
    def get(self, request):
        form = self.form_class(None)
        return render(request, self.template_name, {'form': form})

    def post(self):
        form = self.form_class(request.POST)

        if form.is_valid():
            user = form.save(commit=False)

            #cleaned normalized data

            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

            user.set_password(password)
            user.save()

这里是error

File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/urls/resolvers.py", line 255, in check
    warnings.extend(check_resolver(pattern))
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/urls/resolvers.py", line 172, in check
    warnings = self._check_pattern_startswith_slash()
  File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/urls/resolvers.py", line 140, in _check_pattern_startswith_slash
    regex_pattern = self.regex.pattern

这是我的forms.py文件:

from django.contrib.auth.models import User
from django import forms

class UserForm(forms.ModelForm): # UserForm inherits from forms.
    passwords = forms.CharField(widget=forms.PasswordInput)

    class Meta: # Information about your class.
        model = User # whenevr a creates sign up to your site it's gonna go in same table
        fields = ['username', 'email', 'password']

这是我的urls.py文件:

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

app_name = 'music'

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

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.MEDIA_ROOT)

这是我的album_form.html文件:

{% extends 'music/base.html' %}
{% block title %}Add a New Album{% endblock %}
{% block album_active %}active{% endblock %}

{% block body %}
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12 col-md-7">
            <div class="panel panel-default">
                <div class="panel-body">
                    <form class="horizontal" action="" method="post" enctype="multipart/form-data">
                        {% csrf_token %}
                        {% include 'music/form-template.html' %}
                        <div class="form-group">
                            <div class="col-sum-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-success">Submit</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{%  endblock %}

这是我的index.html文件:

{# loads path to static file #}

{% extends 'music/base.html' %}

{% block body %}

    <ul>
        {% for album in all_albums %}
            <li><a href="{% url 'music:detail' album.pk %}">{{ album.album_title }}</a></li>
        {%  endfor %}
    </ul>

{% endblock %}

1 个答案:

答案 0 :(得分:2)

我怀疑你有一个来自music.urls到urls.py的include的递归引用,因为django抛出的错误是特定于URL解析器的。

您的if语句没有错误。 'music:index'指的是命名空间的url名称,并且仍然需要urls.py中的命名url语句。由于在简单项目中,只有一个应用程序,因此命名空间是多余的。所以在大多数情况下,应该使用'index',就像我在下面显示的那样。

在你的urls.py中,有一个包含到music.urls,它似乎是对它自己的递归引用。 'music.urls'指的是音乐目录中的文件urls.py。 如果你没有'music.urls'的有效python对象,那么你的include语句就错了。

我的项目中没有使用include urls,因此需要为views.py中定义的每个视图创建一个语句。要测试您的服务器是否正确启动,我会尝试以下urlpatterns。不要忘记导入IndexView和DetailView。首先测试1或2后,为其他视图添加更多url语句。

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^music/index/$', IndexView.as_view() , name='Index'),
    url(r'^music/detail/(?P<pk>[0-9]+)/$', DetailView.as_view() , name='Detail'),
]

我使用named url,index.html中的语句应该写成如下:

<a href="{% url 'Detail' album.pk %}">{{ album.album_title }}</a>

命名空间'music:'被省略,因为它是隐式的并且看起来更简单。你应该把它留给简单的应用程序,因为它可能会让初学者感到困惑。