Django show string isn't working - python

I'm passing the manual of Django
I set up my views.py:
from django.http import HttpResponse
from main.views import hello
def hello(request):
return HttpResponse("Hello world")
The file sit in subfolder called : main.
I set up urls.py:
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
url(r'^hello/$', hello),
)
When I'm going to: http://localhost:8000//main/hello or http://localhost:8000/
I see:
NameError at /main/hello name 'hello' is not defined
but if I change my views.py to:
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
)
then in http://http://localhost:8000/ I see :
It worked!
Congratulations on your first Django-powered page.
I don't understand what I'm doing wrong.
These are my project files:
how do I solve this?

You have to import views inside the urls page.
from django.conf.urls.defaults import patterns, include, url
from main.views import hello
urlpatterns = patterns('',
url(r'^hello/$', hello),
)
Now you can access it through:
127.0.0.1:8000/hello

You need to import hello from views.py.
urls.py will look like this.
from django.conf.urls.defaults import patterns, include, url
from main.views import hello
urlpatterns = patterns('',
url(r'^hello/$', hello),
)

Related

Using the URLconf defined in lec3.urls, Django tried these URL patterns, in this order: admin/ hello/ The empty path didn't match any of these

these are my main codes i wrote to create wepapp but i get this 404 error so pleas help
my hello urls.py
from django.urls import path
from hello import views
urlpatterns = [
path("", views.index, name="index")
]
my urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls")),
]
my views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("hello, orld")
It looks like you are trying to access index with
http://127.0.0.1:8000/. This will not work, because your index is included in hello's urls.py, and hello's url starts with hello/.
Try
http://127.0.0.1:8000/hello
instead.

can not import patterns in django 1.11.4

My project urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
My app urls.py is as follows:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('', url(r'^hello/', 'myapp.views.hello', name = 'hello'),)
Now , as soon as I try to run it , it gives me the following error:
from django.conf.urls import patterns, include, url
ImportError: cannot import name 'patterns'
Django doesn't require you to use patterns anymore, you can just make urlpatterns a list of urls
urlpatterns = [url(r'^hello/', 'myapp.views.hello', name = 'hello'),]
Django 1.11 loads that Python module and looks for the variable urlpatterns. This should be a Python list of django.conf.urls.url() instances.
And it runs through each URL pattern, in order, and stops at the first one that matches the requested URL
Dont require patterns more.
urlpatterns should be a Python list of url() instances.

Django - URL routing issues (cannot import name 'urls')

I am following the Django tutorial on https://docs.djangoproject.com/en/1.7/intro/tutorial03/, and am trying to get the index view to show up. I've tried the code specified on the page verbatim but keep on getting errors.
polls/urls.py:
from django.conf.urls import patterns, urls
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
and finally, the index method in views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<h1>Hello world!</h1>");
I'm not sure what I'm doing wrong. I keep getting an error that says "cannot import name 'urls'." any help would be appreciated!
The problem is in your import statement - there is no urls function in django.conf.urls package.
Replace:
from django.conf.urls import patterns, urls
with:
from django.conf.urls import patterns, url

Python import works, but using the imported item fails. Using Django, but this seems like a newbie Python issue

I am running through the Django tutorial.
I have this file
R:\jeffy\programming\sandbox\python\django_files\tutorial\django_test\...
...django_test\article\views.py
Contents:
from django.http import HttpResponse
# Create your views here.
def hello(request):
name = "Mike"
html = "<html><body>Hi %s, this seems to have worked</body></html>" % name
return HttpResponse(html)
And this file:
R:\jeffy\programming\sandbox\python\django_files\tutorial\django_test\...
...django_test\urls.py
Contents:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from article.views import hello
urlpatterns = patterns('',
# url(r'^hello/', include(article.views.hello)),
url(r'^admin/', include(admin.site.urls)),
)
I start the Django server
python manage.py runserver
and go to
http://127.0.0.1:8000
And it works:
As does this
http://127.0.0.1:8000/admin
But when I uncomment the "hello" line, it fails:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from article.views import hello
urlpatterns = patterns('',
url(r'^hello/', include(article.views.hello)), # <--Problem line
url(r'^admin/', include(admin.site.urls)),
)
The error says
R:\\jeffy\\programming\\sandbox\\python\\django_files\\tutorial\\django_test
is in the PYTHONPATH, and the "article" folder is in that directory.
Also, why does the import article line not cause an error, but calling the hello function does fail.
Please help me. What am I missing?
UPDATE
No import at all fails the same way (NameError at / name 'article' is not defined):
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^hello/', include(article.views.hello)), # <-- Problem line
url(r'^admin/', include(admin.site.urls)),
)
Using import article only, causes this error: AttributeError at / 'module' object has no attribute 'views'
from django.conf.urls import patterns, include, url
from django.contrib import admin
import article
urlpatterns = patterns('',
url(r'^hello/', include(article.views.hello)), # <-- Problem line
url(r'^admin/', include(admin.site.urls)),
)
Eliminating the import and putting the call in strings (as suggested), results in an ImportError at / No module named 'article.views.hello'; 'article.views' is not a package:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^hello/', include(article.views.hello)), # <-- Problem line
url(r'^admin/', include(admin.site.urls)),
)
And, finally, importing the hello function directly:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from article.views import hello
urlpatterns = patterns('',
url(r'^hello/', include(hello)),
url(r'^admin/', include(admin.site.urls)),
)
With this, calling http://127.0.0.1:8000 fails but in an expected way:
But then, http://127.0.0.1:8000/hello fails with this:
There's a few things that are wrong here.
1) You are importing something you are not using:
from article.views import hello
...
url(r'^hello/', include(article.views.hello)),
You import hello and then call it from the module article, but article has not been imported and so will be undefined. You can either import article or you can call hello directly.
2) include is not necessary here.
We normally use include for including other urls.py schemas. hello is a function. The way that url-routing works in Django is that you define a route and pass it a function, like so:
from article.views import hello
urlpatterns = patterns('',
url(r'^hello/', hello),
url(r'^admin/', include(admin.site.urls)),
)

Created pages/subpages in Django

I am currently learning Django and I am trying to create a few pages and subpages on my site.
project
urls.py:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'home/', include('home.urls', namespace = 'home')),
url(r'about/', include('about.views', namespace = 'About_page')),
)
I first create a home page:
urls.py:
from django.conf.urls import patterns, url
from home import views
urlpatterns = patterns('',
url(r'^$', views.index, name = 'index'),
views.py:
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render, render_to_response
def index(request):
return render(request,'homepage_template/home.html')
Next I tried to create an about page but I get this error:
Exception Type: ImproperlyConfigured
Exception Value:
The included urlconf <module 'about.views' from '/home/bradford/Development/Django/pub_pic/about/views.pyc'> doesn't have any patterns in it
This is what my about app looks like:
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render, render_to_response
def index(request):
return render(request,'About_template/about.html')
I don't have a urls.py but I thought the line
url(r'about/', include('about.views', namespace = 'About_page')),
would directly include the about.views. However I was wrong because this never called my index() function in about/views.py
I later changed the url() in pub_pic/urls.py to:
url(r'about/', include('about.views.index', namespace = 'About_page')),
But I got this error:
Exception Value:
No module named index
I think this caused due to the fact that only about.views is a module, not index()
I'm not quite sure how to create pages or subpages and best practices for a well structured project. Could someone give me some suggestions please? Thank you!
In your project urls.py, you cannot include an application's view files in urlpatterns.
url(r'about/', include('about.urls', namespace = 'About_page')), )
And in about/urls.py
from django.conf.urls import patterns, url
from about import views
urlpatterns = patterns('',
url(r'^$', views.index, name = 'index'),)
If there is only one url in about/urls.py, then you can directly put it in urls.py of project.
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'home/', include('home.urls', namespace = 'home')),
url(r'^about/$', 'about.views.index', name="About_page_index"),
)

Categories

Resources