how to overcome the following url issue? - django

Hello I am beginner learning Django I am triying to follow this tutorial:
https://docs.djangoproject.com/en/1.11/intro/tutorial01/
I created an app called Polls to test my site:
Since I dont have idea where to put the file called urls.py
I put this file at the following directories:
Django/mysite/polls/urls.py
Django/mysite/polls/migrations/urls.py
This file contains:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
And finally I added the following lines:
at this level:
Django/mysite/mysite/urls.py
this file contains:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]
I dont know where is the issue but when I run:
Django/mysite$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
April 21, 2017 - 15:59:43
Django version 1.10.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
I got in the page the following:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^polls/
The current URL, , didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
So I really appreciate support to overcome this issue,
thanks for the support

the urls.py doesn't go into the migrations folder.
Did you register your Polls app in the settings.py file? you need to add the name of the Polls app to the INSTALLED_APPS list.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'example: add app name here',
'polls',]
you then need to create a url.py file within your polls app.
polls/
urls.py
In that file you would add:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
the url you want to use in your browser would be something like this:
http://127.0.0.1:8000/polls

Related

Local server won't go through my URL pattern on my local server

I'm new to Django and I'm facing a problem that I can't solve despit my research... I'v already made small django projects without this error but since I reformat my computer (I can't see the link but that's context!) i'm having this problem.
So when I'm running a django project this is what my browser shows me despite others urls in my project
Django doesn't go trhough my urls
I'v tried with an almost empty project but a least one more url and still have the same page.
urls.py in the main file (src/mynotes/urls.py):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('index/', include('dbnotes.urls')),
path('admin/', admin.site.urls),
]
settings.py in the main project (src/mynotes/settings.py):
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dbnotes'
]
urls.py in the app (src/dbnotes/urls.py):
from django.urls import path
from . import views
urlpatterns = [
path("", views.homepage, name="homepage"),
]
views.py in the app (src/dbnotes/views.py):
from django.http import HttpResponse
def homepage(request):
return HttpResponse("Hello, world. You're at the polls index.")
Even stranger, when i change "path('admin/', admin.site.urls)" I still get the admin page with the same route. Even if i change 'admin/' or admin.site.urls.
It looks like my local server display an over empty django project but in the error page it's written "mynotes.urls" as the url location which is the good folder.
I'm working with a virtual environment.
I'm lost as it's the first time i got this issue despite having created ohter (small) django projects!
If anyone has an idea that would be great! Thank you!
The directory:
Directory
I tried to follow the django documentation to write the smaller project and then having less room to error!
I tried with a new project and a new virtual envirnoment.
I found a topic related to my problem but without an anwer : Django doesn't see urls.py

How can i fix Page not found 404 in django?

Hi ive been coding my first website and then try running the product page I get this error
*Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/products
Using the URLconf defined in myshop.urls, Django tried these URL patterns, in this order:
admin/
The current path, products, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.*
how can I solve this? Here is my code...
my views page code
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return HttpResponse('Hello world')
def new(request):
return HttpResponse('New Products')
productsurls code
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
path('new', views.new)
]
myshopurls
from django.contrib import admin
from django.urls import path, include
urlpatterns = {
path('admin/', admin.site.urls),
path('products/', include('products.urls'))
}
Most probably, you are facing this problem because you have not include your app "products" in the "settings.py" file.
-> After checking your code I can conclude that"myshop" is your project and "products" is your app.
So, you will have to go in settings.py file under which you will find a list "INSTALLED_APPS".
Inside INSTALLED_APPS -> You will have to include your app.
Go to "apps.py" file of the "products" app and copy the name of the "config" class.
After that in your settings.py file, you will have to write 'products.apps.(paste the name of config class)'.
Most probably name of your config class will be "ProductsConfig" -> So, you will have to write 'products.apps.ProductsConfig',
Hope it will work !
You have to include the app under INSTALLED_APPS in settings.py file.
So your settings.py file should read like so
INSTALLED_APPS = [
# Built In Apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# User Defined Apps
'product.apps.ProductConfig',
]
Tip - If you are confused as to how to include the name under installed Apps. It should be like so
<myapp>.apps.<name_of_class_in_myapp/apps.py_file>
Basically navigate to your app folder -> apps.py file and check the name of the class. It will be like so
from django.apps import AppConfig
class ProductConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'product'
You want the name of this class. ProductConfig.
Note - Local apps should always be added at the bottom because Django executes the
INSTALLED_APPS setting from top to bottom. We want the core Django apps to be available before our app.
The reason for listing the app like
'product.apps.ProductConfig',
is because it is best practice and helps if in future if you decide to use Django Signals.

Page not found error - Writing your first Django app, part 1

I have installed Django 2.2.5 in my conda environment and I'm following the Django tutorial for Writing your first Django app, part 1
I followed the steps exactly but I'm getting a Page Not Found error when I try to access the polls/ url.
My root directory structure is like this
mysite/
manage.py
db.sqlite3
urls.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
polls/
migrations/
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
And my view is like this
D:\TEMP\djangotest\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")
My two urls files are like this
D:\TEMP\djangotest\mysite\polls\urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
D:\TEMP\djangotest\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),
]
And I run the app from the environment like this
(py3.6) D:\TEMP\djangotest\mysite>python manage.py runserver
But when I go to the url indicated in the tutorial it give the 404 error - page not found from the console
October 21, 2019 - 16:23:13
Django version 2.2.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /polls
[21/Oct/2019 16:23:19] "GET /polls HTTP/1.1" 404 1954
From the browser it look like
Page not found (404)
Request Method:
GET
Request URL:
http://localhost: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.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Your main urls file is in the wrong directory. It looks like you've created a separate urls.py in the base "mysite" directory; instead you should have edited the existing one in the "mysite/mysite" directory. The one you've created isn't being used at all.

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',
]

Page not found 404 on Django site?

I'm following the tutorial on Django's site to create a simple poll app. However, Django is unable to resolve "//127.0.0.1:8000/polls" , even though I've defined the regex in mySite/urls.py. I'm doing this in a virtualenv, with the latest Django (1.7) installed.
mySite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
)
mySite/polls/urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
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.")
mySite/settings.py:
...
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
)
....
ROOT_URLCONF = 'mySite.urls'
The error I'm getting:
Using the URLconf defined in mySite.urls, Django tried these URL patterns, in this order: ^admin/
The current URL, polls, didn't match any of these.
I had the same problem.
It turns out I was confused because of the multiple directories named "mysite".
I wrongly created a urls.py file in the root "mysite" directory (which contains "manage.py"), then pasted in the code from the website.
To correct it I deleted this file, went into the mysite/mysite directory (which contains "settings.py"), modified the existing "urls.py" file, and replaced the code with the tutorial code.
In a nutshell, make sure your urls.py file is in the right directory.
Django unable to resolve 127.0.0.1:8000/polls because url config defined as r'^polls/'.
Usual workaround:
mySite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
)
Note:
Whenever Django encounters include(), It chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
mySite/polls/urls.py:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('polls.views',
url(r'^$', 'index', name='index'),
)
Note: Instead of typing that out for each entry in urlpatterns, you can use the first argument to the patterns() function to specify a prefix to apply to each view function.
Answer If
If you want to access 127.0.0.1:8000/polls Note: without trailing slash
use view based url
url(r'^polls', 'polls.views.index', name='index'),
So now you can access 127.0.0.1:8000/polls without trailing slash.
You're accessing to http://yourdomain.com/, and you don't have any URL defined for "/".
You have two options:
If you want to access to the index page of your polls application you have to enter the URL: yourdomain.com/polls
You can also modify you mySite/urls.py file to access from just yourdomain.com
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('polls.urls')),
)
To make the answer clear for beginners who has this issue by following the tutorial, the project root URLconf is the one in the same folder as settings.py which is:
mysite/mysite/urls.py
Just make sure import 'include'. The code looks like:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
]
So in
mysite/mysite/settings.py:
The line should be:
ROOT_URLCONF = 'mysite.urls'
You don't need create a fresh new root URLconf.
Depending on where you put your ROOT urls.py, you set your ROOT_URLCONFIG accordingly, if you have it in your outermost folder containing manage.py then "urls" is ok. if you have it in someother folder then you have to do ".urls"
Credit for the answer to jerryh91
For more info about how it works, check How Django processes a request
You put the urls.py folder into the outer MySite folder, you are suppose to put it in the inner one so its not mySite/urls.py, but mySite/mySite/urls.py:
ran into the same mistake when i did the tutorial
Another way to access 127.0.0.1:8000/polls would be to redirect the browser when accessing 127.0.0.1:8000. It is done by editing .../mysite/mysite/urls.py as follows:
from django.conf.urls import include, url
from django.contrib import admin
from polls import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include('polls.urls', namespace='polls')),
url(r'^$', views.index, name='index'),
]
Page not found?
If you get an error page here, check that you’re going to http://localhost:8000/polls/ and not http://localhost:8000/.
Source : https://docs.djangoproject.com/en/3.0/intro/tutorial01/
Actually the problem is that you didn't notice that
mysite/urls.py and polls/urls.py are two different files and you modified polls/urls.py instead of putting mysite/urls.py in the urls.py file in ...mysite\mysite folder.
In my case, it was a stupid mistake. I wanted to integrate the plugin django-tinymce, and test it. So following this guide, I did the step 3 and exported the variable to the path. As the server runned again, I received the not found error, showing the message:
Using the URLconf defined in testtinymce.urls, Django tried these URL
patterns, in this order: ....
But I didn't know what exactly it was, until I remembered exporting the variable DJANGO_SETTINGS_MODULE
running unset DJANGO_SETTINGS_MODULE in terminal solved my issue. Hope that it helps someone too.
Add the below line in your Mysite/urls.py
url(r'^$', views.index, name='index'),
and check. If you have created your project correctly, it should work. Else something like above might have happened to have more than one files so confused.
2017-10-05_12:03 ~/mysite/mysite
$ vi urls.py
2017-10-05_12:04 ~/mysite/mysite
$ cd ../..
2017-10-05_12:04 ~
$ mv mysite SENSIBLE_NAME_DJANGO_ROOT
i had the same issue and got it resolved by adding /polls after http://server:port/ and so final address in server looks like:
http://server:port/polls