django 1.11 polls app not working - django

i am new to django and web frameworks. As said in the documentation of django 1.11.4, i changed polls/view.py as
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
after that i created polls/urls.py and write code in it as:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
finally changed mysite/urls.py as
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
when i run this project with: python manage.py runserver
it shows error when go to "http://localhost:8000/polls/":
Not Found: /polls/
[09/Aug/2017 12:01:36] "GET /polls/ HTTP/1.1" 404 1947
i add polls to installed apps. What else to do ??

Path error. Problem solved. code should be in "mysite/mysite/urls.py" not in "mysite/urls.py"

Probably you did'nt include the polls app properly in your project. Kindly post that line of code.

Related

Django not using updated urls.py - returning 404 on www.site.com/page with outdated list

I am very new to django and beginning to understand some of the framework however view-route binding is confusing me
There is a persistent issue that when I try to visit any url except for the homepage and /admin I receive a 404, including routes I have declared in my project's urls.py file
also i am following this mdn tutorial
project urls.py
"""trends URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.views.generic import RedirectView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', include('articles.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
app named 'articles' urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
app named 'articles' views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the articles index.")
and here is the 404 page I receive
I know this is becoming very long but there is one more odd thing, when I refresh the 404 page, it will toggle between showing me the above screenshot and sometimes show me an old route which is no longer in the urls.py like this
this is on an nginx server with gunicorn, and restarting the nginx service does not solve the issue
In your projects urls.py you have defined
path('articles/', include('articles.urls')),
So by going to YOUR_URL/articles will not give a valid response. Instead try going to YOUR_URL/articles/ or change your path to
path('articles', include('articles.urls')),
Stumbled upon this SO post which lead me to the idea to restart gunicorn and that solved my problem so try running
sudo service gunicorn restart
should fix your problems

how to correctly import urls.py from app?

This is probably pretty simple but I can't get my head around it. I'm learning Django, have v3.0.4 installed and can't get the URLs from an app to work correctly.
On the project urls.py I have the following:
Project\urls.py:
from django.contrib import admin
from django.urls import path
from django.urls import include
from AppTwo import views
urlpatterns = [
path('', views.index, name='index'),
path('', include('AppTwo.urls')),
path('admin/', admin.site.urls),
]
I've created an app named "AppTwo" and have the following urls.py and views.py in the app:
AppTwo\urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('/help', views.help, name='help'),
]
AppTwo\views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<em>My Second App</em>")
def help(request):
return HttpResponse("<em>Help Page!!!</em>")
If I browse to http://127.0.0.1:8000/ the index page loads and I see the text "My Second App" as expected. However if I browse to http://127.0.0.1:8000/help I get page not found 404 error.
I can also browse to the admin page just fine. So far this is a stock project, the only other change I made after creating it was to the settings.py file to install the "AppTwo" application. Based on the documentation, this looks like it should work, so what am I doing wrong?
yep, knew it was simple.
Changed
path('/help', views.help, name='help'),
to:
path('help/', views.help, name='help'),
all good now.

Error while using URLconf defined in mysite.urls (Django tutorial)

I know this question has been asked and i have gone through all those answers but i didn't get the solution still. Please help me find out what is wrong .
This is the code from django website for version 1.11 of django .
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
mysite/polls/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
mysite/polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
I am getting the following error
Click to view the error
Your urls.py is in the wrong place, it should be mysite/mysite/urls.py (in the same directory as settings.py).

Django polls app is not working as expctected in the tutorial for me

I am getting this error every time restart my server
Page not found (404) Request Method: GET Request URL:
http://127.0.0.1:8000/polls/
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order: admin/ The current path, polls/, didn't
match any of these.
I have tried restarting the server but the same error is popping up every time.
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name = 'index'),
]
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("polls/", include("polls.urls")),
path("admin/", admin.site.urls)
]
views.py/polls
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world. You're at the polls index.")
The expected result as per the Django tutorial is the text "Hello world. You're at the polls index." after you start your server
You probably didn't add your app polls to installed apps in your settings.py.
Open your file settings.py and write:
INSTALLED_APPS = [
....,
'polls',
]

Django Project App Urls Page Not Found (404)

I still new to Django and I'm following this tutorial in YouTube. I can't proceed to the next tutorial because I hit an error.
When I type http://127.0.0.1:8000/music. I get this error.
Check my code below:
mysite urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^music/', include('music.urls')),
]
music urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
music views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Hello World</h1>")
Please help.
You might have not saved the project/urls.py file.
Or to check whether the music is set in INSTALLED_APPS in settings.py?