html file urls is not accessible in django - python

register.html is not accessible on 'register/' url
tried changing url to '' and webpage is accessible but not on 'register/'
projects urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('login/',include('login.urls')),
path('admin/', admin.site.urls),
]
apps urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.indexView, name = "home"),
path('dashboard/', views.dashboardView, name="dashboard"),
#path('login/',),
path('register/',views.registerView, name="register_url"),
#path('logout/',),
]
views.py
from django.shortcuts import render
def indexView(request):
return render(request,'index.html')
def dashboardView(request):
return render(request,'dashboard.html')
def registerView(request):
return render(request,'register.html')
templates/register.html

Either you type login/ before evry URL pattern of apps urls.py file while testing URL.
or
You should include url mapping of apps urls.py in main urls.py
This might help you.
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('',include('apps.urls')), #put you app name here
path('admin/', admin.site.urls),
]
apps urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.indexView, name = "home"),
path('dashboard/', views.dashboardView, name="dashboard"),
path('register/',views.registerView, name="register_url"),
]
This will work and you can add login/ url mapping inside the apps urls.py.
This type of flow will make it easy to understand .

Use url_name for URL calling:
{% url 'register_url' %}
This will directly search for 'register_url' in the project and will return the path.
url_name must be unique so make sure your url_name is uniquely defined.

Related

Error in Django: Using the URLconf defined in django_test.urls, Django-3 tried these URL patterns, in this order:

I've tried setting these URL's in this project, but I get a 404 when trying to access the registration.html file and the api-auth URL that is related to a REST API
For reference:
index = app
django_test = site
index/urls.py
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('', views.homepage, name="homepage"),
path('/register', views.registration, name="registration"),
path('api', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace="rest framework")),
]
django_test/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('index.urls')),
path('admin/', admin.site.urls)
]
index/views.py
from django.shortcuts import render
def homepage(request):
return render(request,"home/homepage.html")
def registration(request):
return render(request, 'home/registration.html')
All of our HTML files are located in index/templates/home directory
Can you try changing name="registration" to name="register" ?
Change the URL "register/" to "/registration"

How to Change Django Default Page to your Design?

I uploaded my site to the live server, but still, I am seeing Django default page, I updated my urls.py file, but still, it's not working. and it's showing me this output. Please let me know where I am Mistaking.
I am trying to access this mydomain.com/dashboard
Using the URLconf defined in yoursite.urls, Django tried these URL patterns, in this order:
admin/
The current path, dashboard, didn't match any of these.
here is my urls.py file...
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^dashboard/', include('dashboard.urls')),
url(r'^accounts/', include('accounts.urls')),
url('', include('frontpanel.urls')),
path('', views.index, name='index'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
And here is my views.py file...
def index(request):
category = Category.objects.all().order_by('created_at')
return render(request, "base.html", {'category':category})
What version of Django are you using?
Try changing url to re_path (remember to import it first). I think url has been deprecated.
After Analyzing the question u never mentioned ur app urls.py you only mentioned project urls.py . As u have url(r'^dashboard/', include('dashboard.urls')), in ur project urls.py, there must be a file in your app also named urls.py which handles urls having prefix /dashboard/ , by default django doesnt make that file you need to manually make it add ur function names to redirect . For example this is my main urls.py file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('display_report/', include("display_report.urls"))
]
then u have to make a file named urls.py in ur app also to handle the requests and redirect the the approaite functions , in my display_report app i made a urls.py that looks like this
from django.contrib import admin
from django.urls import path, include
from display_report import views
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [path('', views.index)]
And then it will redirect to function named index in ur views.py file inside ur app
from django.shortcuts import render, redirect, HttpResponse
# from .models import Employee, Tasks, Report, Fileupload, Fileuploadnext
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
def index(request):
return render(request."index.html")
Here my ur will be mydomain.com/display_report and my index.html file will be inside the template folder

Url in template from site-package

i'm building my first Django app so i'm know i'm missing a lot of things, but i installed a gallery for my site and is inside site-packages, i set the url inside urls.py and it looks like this :
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from contacto.views import Home,Contacto
urlpatterns = [
path('admin/', admin.site.urls),
path('gallery/', include('gallery.urls'),name='gallery'),
path('home/',Home,name='home'),
path('contacto/',Contacto,name='contacto')
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I can perfectly access the gallery and its functionalities if i write the url in the browser, but i cannot reference the gallery Url inside my template like the other Urls using {% url 'gallery' %}, i keep getting this error :
Reverse for 'gallery' not found. 'gallery' is not a valid view function or pattern name.
Also heres the gallery url:
from django.urls import path
from gallery.views import ImageView, ImageList, AlbumView, AlbumList, ImageCreate
app_name = 'gallery'
urlpatterns = [
path('', AlbumList.as_view(), name='album_list'),
path('images/', ImageList.as_view(), name='image_list'),
path('image/<int:pk>/<slug>', ImageView.as_view(), name='image_detail'),
path('upload/', ImageCreate.as_view(), name='image_upload'),
path('album/<int:pk>/<slug>/', AlbumView.as_view(), name='album_detail'),
path('album/<int:apk>/<int:pk>/<slug>', ImageView.as_view(), name='album_image_detail')
]
Thanks!
app_name = 'gallery'
urlpatterns = [
path('admin/', admin.site.urls),
path('gallery/', include('gallery.urls')),
path('home/',Home,name='home'),
path('contacto/',Contacto,name='contacto')
]
Change it as shown above and call gallery:album_list for album list, and follow same pattern for others

Cannot import name 'views',.Python, Django

I have read many answers in this forum, but they does not solve my problem. I will be very grateful for help.
My file views.py returns this error:
from . import views
ImportError: cannot import name 'views' from '__main__' (C:/Users/tymot/Desktop/weather app/env/Environemnt/the_weather/weather/views.py)
views.py (Environemnt\the_weather\weather)
from django.shortcuts import render
from django.contrib import admin
def index(request):
return render(request, 'weather/index.html') #returns the index.html
urls.py (Environemnt\the_weather\weather)
from django.urls import path
from . import views
urlpatterns = [
path('', views.index), #the path for our index view
]
urls.py (Environemnt\the_weather\the_weather)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('weather.urls')),
templates(the_weather\weather\templates\weather)
only file index.html
Directory
-the_weather
--the_weather
---__init__
---setting
---urls
---wsgi
--weather
---migrations
----__init__
---templates
----weather
-----index
---__init__
---admin
---apps
---models
---tests
---urls
---views
--db
--manage.py
I try use to resolved my problem from __future__ import absolute_import, or homepage import views. I else try copy views.py to directory templates (and modify its code) but unfortunately it not work
You need to separate your views and urls create a new module (file) urls.py in your app, in your case it is weather folder, and add these code there, and remove it from views.py, you can read here about it to understand it better.
Path : the_weather/weather/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index), #the path for our index view
]
Path : the_weather/weather/views.py
from django.shortcuts import render
from django.contrib import admin
def index(request):
return render(request, 'weather/index.html') #returns the index.html template
You would need to put your views.py file in your weather folder. Make sure it is in the right weather folder, I am guessing you have two weather folders. Also make sure these codes are right;
#Path : the_weather/weather/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index), #the path for our index view
]
#Path : the_weather/weather/views.py
from django.shortcuts import render
from django.contrib import admin
def index(request):
return render(request, 'weather/index.html') #returns the index.html template
Why don't you try like this
from .views import index
There may be separate folder of urls and views.py
what you have to do is you have to write
from appname import views
suppose your app name is myapp
you have have to write
from myapp import views
use should create new url.py in weather and write code in views.py
url.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
write code in urls.py of your project
from django.urls import path, include
from . import views
urlpatterns = [
path('wheather/',include('wheather'))
]
I had Similar problem, it didn't solved, till I added an empty views.py in root in example, in your case add to Environemnt\the_weather\the_weather
I had the same problem.
In formsdemo\formsdemo\__init__.py, I changed the import code to from forms app import views.
from django.contrib import admin
from django.urls import path
import main.views
urlpatterns = [
path('admin/', admin.site.urls),
path('', main.views.home_page, name="home_page"),
]
Well, i was also facing the same issue. I was continuously getting server errors but then I figured out it was a simple error.
go to the urls.py file
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name = index), #the path for our index view
]
I just had to remove name = index and the server worked with 0 errors.
enter image description here

How to setup urls.py to work with app/urls.py and templates in src with Django

Trying to figure out how to setup my own project.
I created a new Django app to make a homepage.
src/home/urls.py:
from django.conf.urls import url
urlpatterns = [
url(r'^$', 'views.index', name='index'),
]
src/home/views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, "index.html", {})
src/project/urls.py:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^', include('home.urls')),
url(r'^admin/', admin.site.urls),
]
src/templates/index.html:
<h1>Hello World</h1>
The reason this isn't in a templates folder inside of the home app is because I want to use this as my base template eventually for all apps and pages
Error reads:
ImportError at /
No module named 'views'
Using python 3.5 and django 1.9
EDIT*** changed to home.views.index
ERROR now reads:
TemplateDoesNotExist at /
index.html
In Site-wide Urls.py do the Following:
from app_name import urls as app_name_urls
from django.conf.urls import include
urlpatterns=[
path('',include(app_name_urls)
]
In App/urls.py
from django.urls import path
from .import views
urlpatterns=[
#your paths go here
]
Make sure you home is a package and you have __init__.py there.
You might also need to change views.index to home.views.index in your urls.py

Categories