Reputation: 37
I'm fairly new to python and django. I do know what this error indicates though. It is saying I need to import views into the urls. The strange thing is on my laptop it runs fine and loads the page, but on my desktop it isn't. I am using a git repo, and just pulled the working changes from my laptop. I use the same Python interpreter on both machines(3.5.3).I get the same error message for all of the urls as the title states.
Here is the Full Stack
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().
matchmaker(project/main)/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'),
]
newsletter/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)
matchmaker/views.py
from django.shortcuts import render
def about(request):
return render(request, "about.html", {})
I am hoping for a little assistance or guidance. whenever I do something like this:
from app_name import views
from newsletter.views import home, contact
Pycharm greys it out, and says it is an unused import statement. In the couple other projects I have worked on, I haven't needed to import any of my views into the main/project urls.py .
Upvotes: 1
Views: 1815
Reputation: 308979
You've imported the views to your urls as required, but you've missed out the next step to use the views in your url patterns instead of the dotted string paths
from newsletter.views import home, contact
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^contact/$', contact, name='contact'),
...
]
Once you use a view in a url pattern, PyCharm should no longer mark it as an unused import.
Support for dotted string paths was removed in Django 1.10. If your code works on one machine but not another, then it sounds like you are running two different versions of Django. It's important to try to install the same package versions on all machines to avoid issues like this. Find out about pip freeze
and requirements files if you haven't already (try this article).
Upvotes: 1
Reputation: 1618
Your home
method does not return
a rendered template yet, which is expected.
return render(request, "home.html", context)
Rendering your home
view should not work in the current situation on your laptop or anywhere else. As far as I know the older versions would all fail too.
Also Django does not support import paths since 1.10, but you should import the views, and pass them to your url's.
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'),
]
You should be better off splitting your of the different apps into differen urls.py
file of every app, to prevent the namespacing conflicts.
Upvotes: 0