I am trying to create a web app using Django framework. I followed the tutorial and did everything as per the tutorial.
DJANGO_PROJECT:
from django.contrib import admin
from django.urls import include
from django.conf.urls import url
urlpatterns = [
url(r'^dellserver/', include('dellserver.urls')),
url(r'^admin/', admin.site.urls),
]
Settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dellserver'
]
dellserver\urls.py:
from . import views
from django.conf.urls import url
urlpatterns = [
url(r'^dellserver/$', views.index, name="index")
]
views.py:
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Hello World</h1>")
Can anybody tell me what I am doing wrong? Why I a getting the **
Page not found (404)
**
You did the mistake in project's url (folder which include settings.py file)
from django.contrib import admin
from django.urls import include
from django.conf.urls import url
urlpatterns = [
url(r'^/', include('dellserver.urls')), # this line you did the Mistake
url(r'^admin/', admin.site.urls),
]
in main urls remove dellserver as shown below.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('dellserver.urls')),
]
so that you can call your url ../dellserver/, otherwise you have to call it ../dellserver/dellserver twice.
Related
Hi, I have Blog post project some functionalities made with DRF and some functionalities made with Django.I need to add 404 page for missing urls. But If I use right-working link, I will get server error, However, I will get 404 page for missing url.
For example, urls such as '127.0.0.1:8000/en/me' or '127.0.0.1:8000/en/me/contact/' or '127.0.0.1:8000/en/book' will bring server error,
but url '127.0.0.1:8000/en/men' return not found page
in my django_project/urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from django.conf.urls.i18n import i18n_patterns
# from django.conf.urls import handler404
from my_works import views as my_works_views
schema_view = get_schema_view(
openapi.Info(
title="API Docs",
default_version='v1',
description="API urls description",
terms_of_service="https://www.myblogs.com/policies/terms/",
contact=openapi.Contact(email="aahmadov271101#gmail.com"),
license=openapi.License(name="Test License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
urlpatterns = [
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
path('i18n/', include('django.conf.urls.i18n')),
]
urlpatterns += i18n_patterns (
path('', include('my_works.urls')),
path('me/', include('accounts.urls')),
path('admin/', admin.site.urls),
)
urlpatterns+= (static(settings.STATIC_URL, document_root=settings.STATIC_ROOT))
urlpatterns+= (static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT))
handler404 = 'accounts.views.error_404_view'
admin.site.site_header = 'Alimardon Mustafoqulov Administration'
admin.site.site_title = 'Administration'
admin.site.index_title = 'Administration page'
in my django_app/urls.py:
from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns
from .views import (
ArticlesViewList,
BooksViewList,
PresentationsViewList,
ProjectsViewList,
EventsViewList,
VideosViewList,
)
from rest_framework import routers
from django.conf.urls import handler404
router = routers.DefaultRouter()
router.register(r'articles', ArticlesViewList, basename='articles')
router.register(r'books', BooksViewList, basename='books')
router.register(r'presentations', PresentationsViewList, basename='presentations')
router.register(r'projects', ProjectsViewList, basename='projects')
router.register(r'events', EventsViewList, basename='events')
router.register(r'videos', VideosViewList, basename='videos')
urlpatterns = []
urlpatterns += router.urls
in my django_app2/urls.py:
from django.urls import path, include
from .views import ContactAPIView, ProfileView, AdminContactView, AddressLinkView
from rest_framework import routers
from django.contrib.auth import views as auth_views
from rest_framework.authtoken.views import obtain_auth_token
router = routers.DefaultRouter()
router.register(r'phones',AdminContactView, basename='phone')
router.register(r'addresses',AddressLinkView, basename='addresses')
router.register(r'contact',ContactAPIView, basename='contact')
router.register(r'',ProfileView, basename='profile')
urlpatterns = [
# path('auth-token/', obtain_auth_token, name='token-auth'),
path('api-auth/', include('rest_framework.urls')),
path('password-reset/',
auth_views.PasswordResetView.as_view(),
name='password_reset'),
path('password-reset/done/',
auth_views.PasswordResetDoneView.as_view(
template_name='password_reset_done.html'
),
name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(),
name='password_reset_confirm'),
path('password-reset-complete/',
auth_views.PasswordResetCompleteView.as_view(),
name='password_reset_complete'),
]
urlpatterns += router.urls
in my django_project/settings.py:
import os
from pathlib import Path
from django.utils.translation import gettext_lazy as _ #for multi-language
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'secret key hidden'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ["*",]
# Application definition
INSTALLED_APPS = [
'modeltranslation',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts.apps.AccountsConfig',
'my_works.apps.MyWorksConfig',
'crispy_forms',
'rest_framework',
'django_filters',
'rest_framework.authtoken',
'corsheaders',
'phonenumber_field',
'drf_yasg',
'whitenoise',
]
I seem to find the following error despite making a number of changes in my app.urls file and in my project.urls file. I can't seem to put my finger on it, here is the code:
app.urls file
from django.urls import path
from . import views
urlpatterns = [
path('', views.home_page, name = 'home_page'),
]
project.urls file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('Success.urls')),
]
views.py file
from django.shortcuts import render
from django.http import HttpResponse
enter code here
Create your views here.
def home_page(request):
hello = 'hello world'
return HttpResponse (hello)
Have you added your app in the settings.py file?
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Success',
]
i imported render and used it instead i can't figure out though why it worked
views.py file
from django.shortcuts import render
def hello_world(request):
return render(request, 'hello_world.html')
app/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.hello_world, name = 'hello_world')
]
project/urls.py
from django.contribs import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app.urls'))
]
I am following the official Django tutorial on https://docs.djangoproject.com/en/2.2/intro/tutorial01/ but somehow I am not able to run the server as I have created the polls app and added the required urls. When I use the command "py mysite\manage.py runserver" it returns me ModuleNotFoundError: No module named 'polls' error.
Project Folder available at
https://i.stack.imgur.com/bbxfW.png
#views.py
from django.http import HttpResponse
def index(request):
return HttpResponse('<h1><this is a test page</h1>')
#urls.py in polls
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
#urls.py in mysite\mysite
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
#settings.py
INSTALLED_APPS = [
'polls',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
[1]: https://i.stack.imgur.com/bbxfW.png
Well, I resolved it myself. Polls module was not found because it was created outside the project directory. When I removed it and recreated inside the project directory, it was OK now.
also it may be a good idea to cd into yoru django project so you are only running
python manage.py runserver
I use Pycharm,django2.0.4
I started Django yesterday.
I faced one error.
Using the URLconf defined in chatbottest.urls, Django tried these URL patterns, in this order:
1. admin/
2. globalHaksik/
The empty path didn't match any of these.
enter image description here
setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'globalHaksik',
]
chatbottest\urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('globalHaksik/', include('globalHaksik.urls')),
]
globalHaksik\urls.py
from django.urls import path
from . import views
urlpatterns = [
path('keyboard/', views.keyboard),
]
views.py
from django.http import JsonResponse
def keyboard(request):
return JsonResponse({
'type': 'buttons',
'buttons': ['학식', '배달음식']
})
I do not know the correct answer if I look for the same error on this site.
Please kindly answer me.
Update: Looks like you are just trying to access the URL leading to views.keyboard. So the url that you need to access is /globalHaksik/keyboard/ (and not just /keyboard) since the path defined in chatbottest\urls.py has globalHaksik/ in it:
path('globalHaksik/', include('globalHaksik.urls')),
If you are trying to access /, then you need to add the empty pattern as follows:
chatbottest\urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', views.your_view_name),
path('admin/', admin.site.urls),
path('globalHaksik/', include('globalHaksik.urls')),
]
If you are trying to access /globalHaksik/, then you you need to add the empty pattern to globalHaksik\urls.py:
globalHaksik\urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.your_view_name),
path('keyboard/', views.keyboard),
]
I've reviewed other posts related to my question, but I'm unable to find an answer for my question.
When I try to access "http://127.0.0.1:8000/TEST_PAGE" I get the following error:
"Using the URLconf defined in TEST_PAGE, Django tried these URL patterns, in this order:
^admin/
^$
^TEST_PAGE/
The current URL, TEST_PAGE, didn't match any of these."
Here is what I have for Mysite/Mysite/urls.py:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', include('Main_Page.urls')),
url(r'^TEST_PAGE/', include('TEST_PAGE')),
]
I have the APP installed in Mysite/Mysite/settings.py:
INSTALLED_APPS = [
'TEST_PAGE',
'Main_Page',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Here is what I have for Mysite/TEST_PAGE/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^TEST_PAGE', views.index, name='TEST_PAGE'),
]
And here is what I have for Mysite/TEST_PAGE/views.py:
from django.shortcuts import render
def index(request):
return render(request, 'TEST_PAGE/TEST_PAGE.html')
The main/index/home app works fine, I'm just trying to access the second app.
I'm new to this, so any help would be appreciated.
By including 'TEST_PAGE' twice(in both urls.py), you have defined a url:
/TEST_PAGE/TEST_PAGE
So
django can find url /TEST_PAGE/TEST_PAGE but not /TEST_PAGE
Your Mysite/TEST_PAGE/urls.py should be:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='TEST_PAGE'), # maps to /TEST_PAGE/
]