2017-03-10 67 views
1

我對python和django相當陌生,但我知道這個錯誤是什麼意思,它是說我需要導入在我的筆記本電腦上它運行良好並加載頁面,但在我的桌面上它不是。我正在使用一個git repo,並從我的筆記本電腦中拉出工作變化。在兩臺機器上Python解釋器(3.5.3)。我得到同樣的錯誤信息所有的URL作爲標題的狀態。raise TypeError('在include的情況下'必須是可調用的或列表/元組'

下面是完整的堆棧

Unhandled exception in thread started by <function check_errors. 

<locals>.wrapper at 0x000001FA9FAB7400> 
Traceback (most recent call last): 
    File "C:\Users\Rpg Legend\mm\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper 
    fn(*args, **kwargs) 
    File "C:\Users\Rpg Legend\mm\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run 
    self.check(display_num_errors=True) 
    File "C:\Users\Rpg Legend\mm\lib\site-packages\django\core\management\base.py", line 374, in check 
    include_deployment_checks=include_deployment_checks, 
    File "C:\Users\Rpg Legend\mm\lib\site-packages\django\core\management\base.py", line 361, in _run_checks 
    return checks.run_checks(**kwargs) 
    File "C:\Users\Rpg Legend\mm\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks 
    new_errors = check(app_configs=app_configs) 
    File "C:\Users\Rpg Legend\mm\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config 
    return check_resolver(resolver) 
    File "C:\Users\Rpg Legend\mm\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver 
    for pattern in resolver.url_patterns: 
    File "C:\Users\Rpg Legend\mm\lib\site-packages\django\utils\functional.py", line 35, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "C:\Users\Rpg Legend\mm\lib\site-packages\django\urls\resolvers.py", line 313, in url_patterns 
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
    File "C:\Users\Rpg Legend\mm\lib\site-packages\django\utils\functional.py", line 35, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "C:\Users\Rpg Legend\mm\lib\site-packages\django\urls\resolvers.py", line 306, in urlconf_module 
    return import_module(self.urlconf_name) 
    File "C:\Python35\lib\importlib\__init__.py", line 126, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 986, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 969, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked 
    File "<frozen importlib._bootstrap>", line 673, in _load_unlocked 
    File "<frozen importlib._bootstrap_external>", line 673, in exec_module 
    File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed 
    File "C:\Users\Rpg Legend\PycharmProjects\matchmaker\src\matchmaker\urls.py", line 9, in <module> 
    url(r'^$', 'newsletter.views.home', name='home'), 
    File "C:\Users\Rpg Legend\mm\lib\site-packages\django\conf\urls\__init__.py", line 85, in url 
    raise TypeError('view must be a callable or a list/tuple in the case of include().') 
TypeError: view must be a callable or a list/tuple in the case of include(). 

媒人(項目/主)/urls.py:

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

urlpatterns = [ 
    url(r'^$', 'newsletter.views.home', name='home'), 
    url(r'^contact/$', 'newsletter.views.contact', name='contact'), 
    url(r'^question/$', 'questions.views.home', name='question_home'), 
    url(r'^about/$', 'matchmaker.views.about', name='about'), 
] 

通訊/ views.py

from django.conf import settings 
from django.core.mail import send_mail 
from django.shortcuts import render 
from questions.models import Question 

from .forms import ContactForm, SignUpForm 


# Create your views here. 
def home(request): 
    title = 'Sign Up Now' 
    form = SignUpForm(request.POST or None) 
    context = { 
     "title": title, 
     "form": form 
    } 


def contact(request): 
    title = 'Contact Us' 
    title_align_center = True 
    form = ContactForm(request.POST or None) 

    if form.is_valid(): 
     form_email = form.cleaned_data.get("email") 
     form_message = form.cleaned_data.get("message") 
     form_full_name = form.cleaned_data.get("full_name") 
     subject = 'Site contact form' 
     from_email = settings.EMAIL_HOST_USER 
     to_email = [from_email, '[email protected]'] 
     contact_message = "%s: %s via %s" % (
      form_full_name, 
      form_message, 
      form_email) 
     some_html_message = """ 
      <h1>hello</h1> 
     """ 

     send_mail(subject, 
       contact_message, 
       from_email, 
       to_email, 
       html_message=some_html_message, 
       fail_silently=True) 

    context = { 
     "form": form, 
     "title": title, 
     "title_align_center": title_align_center, 
    } 
    return render(request, "forms.html", context) 

媒人/ views.py

from django.shortcuts import render 

def about(request): 
    return render(request, "about.html", {}) 

我我希望有一點幫助或指導。每當我做這樣的事情:

from app_name import views 
from newsletter.views import home, contact 

Pycharm灰色,並說這是一個未使用的導入聲明。在我已經開發的其他項目中,我不需要將任何視圖導入到主/項目urls.py中。

+0

在matchmaker/views.py中:你應該縮進「about」函數的內容。但是,我不知道你的問題的解決方案是什麼。 – Ambitions

回答

1

您已經導入了意見,您的網址作爲必需的,但你已經錯過了使用的意見,您的網址模式,而不是虛弦路徑的下一步

from newsletter.views import home, contact 

urlpatterns = [ 
    url(r'^$', home, name='home'), 
    url(r'^contact/$', contact, name='contact'), 
    ... 
] 

一旦你使用在一個url模式下查看,PyCharm不應該將其標記爲未使用的導入。

在Django 1.10中刪除了對虛線字符串路徑的支持。如果你的代碼在一臺機器上運行,但不在另一臺機器上運行,那麼聽起來你正在運行兩個不同版本的Django。嘗試在所有機器上安裝相同的軟件包版本以避免這類問題非常重要。如果還沒有(try this article),請查找pip freeze和需求文件。

+0

感謝您的幫助。那就是訣竅。在某一點上,我喜歡這樣,但保持在家中,並以字符串值引號。奇怪的是,我的筆記本電腦上的版本必須是已棄用的Django版本?因爲舊的語法和新的語法都適用於我的筆記本電腦。我還不夠了解Pycharm中的python解釋器如何處理這個問題。它是Django過時的警告還是通知?據我所知,我只使用了1.10,但我可以同時使用這一事實似乎是這種情況的唯一解釋? –

+0

您可以使用'python manage.py version'或'pip freeze'來檢查已安裝的Django版本。 Django的舊版本不一定不推薦使用。 Django 1.8.x是一個長期的支持版本,至少支持到2018年4月,而Django 1.9.x仍然支持,直到下個月發佈Django 1.11。 – Alasdair

+0

我不明白你的問題,「這是一個警告或Django已過時的通知」。 PyCharm將使用安裝的Django版本。據我所知,如果您使用的是過期版本,則不會警告您。對進口進行格式化只是提示您可能能夠刪除導入並保持模塊整潔。 – Alasdair

0

您的home方法沒有return尚未呈現的模板,這是預期的。

return render(request, "home.html", context) 

渲染你的home視圖應該在當前形勢下不工作在你的筆記本電腦或其他任何地方。據我所知,舊版本都會失敗。

此外,自1.10開始,Django不支持導入路徑,但您應該導入視圖並將它們傳遞給您的url。

from newsletter import views as newsletter_views 
from questions import views as question_views 
from matchmaker import views as matchmaker_views 

urlpatterns = [ 
    url(r'^$', newsletter_views.home, name='home'), 
    url(r'^contact/$', newsletter_views.contact, name='contact'), 
    url(r'^question/$', question_views.home, name='question_home'), 
    url(r'^about/$', matchmaker_views.about, name='about'), 
] 

你應該會更好分裂不同的應用程序的您到differen urls.py文件每一個應用程序中,以防止命名空間衝突。

相關問題