The structure of my project is this:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
Home/
templates/
Home/
Home.html
Login/
templates/
Login/
Login.html
Application/
templates/
Application/
Application.html
Details.html
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('Home.urls', namespace="Home")),
url(r'^Application', include('Application.urls',
namespace="Application")),
url(r'^Login', include('Login.urls', namespace="Login")),
url(r'^User', include('User.urls', namespace="User")),
]
Home/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^', views.Home, name = 'Home'),
]
Application/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'Application', views.Application, name = 'Application'),
]
The structure for the views.py(s) is the same for each app (with changes in names, of course).
Home/views.py
from django.shortcuts import render
def Login(request):
return render(request, 'Login/Login.html')
I have two links in my Home.html. One is supposed to direct a user to Application.html and the other, to Login.html. I tried to do the followings:
templates/Home/Home.html
Apply Now!
Apply Now!
Login
None of them work. No errors, no redirections, nothing.
What is weird about this whole thing is that url changes for Login and Application look different, even though the structure of both apps are the same:
The url changes to:
http://localhost:8000/ApplicationApplication.html
vs
http://localhost:8000/Login/Login.html.
Would greatly appreciate any help!
mysite / urls.py
url(r'^Application', include('Application.urls',
namespace="app")),
Application/Application.urls
urlpatterns = [
url(r'Application', views.Application, name = 'application'),
]
home.html
Apply Now!
Please change to retry
Related
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
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.
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
I'm new in Django and use this tutorial for learning https://docs.djangoproject.com/en/1.6/intro/tutorial03/
Have a little problem with routing in.
View code:
def index(request):
return HttpResponse(r'<h3 style="font-style: bold;">Index</h3>')
URL config code:
1. blog/urls
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
2.project/urls
urlpatterns = patterns('',
url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
When i come for 127.0.0.1:8080/index that
Page not found (404)
Using the URLconf defined in les1.urls, Django tried these URL patterns, in this order:
^blog/
^admin/
The current URL, index, didn't match any of these.
Struct of project
blog/
templates
__init__.py
admin.py
models.py
tests.py
urls.py
views.py
les1/
__init__.py
settings.py
urls.py
wsgi.py
db.sqlite3
manage.py
Can't find error:(
What is the actual url you enter in your browser ? is it 127.0.0.1:8000 or 127.0.0.1:8000/index ? From your post, I assume it is the second:
The current URL, index, didn't match any of these.
What if you enter 127.0.0.1:8000/blog ?
index is the name of your route, used only on server-side. It's a shortcut used for quick reversing and displaying of URL:
from django.core.urlresolvers import reverse
print(reverse('index'))
# will output "/blog", which is the actual URL
Remove r from view code.
def index(request):
return HttpResponse('<h3 style="font-style: bold;">Index</h3>')
There are many similar questions posted already, but I've already tried those solutions to no avail. I'm working through a basic Django tutorial, and here is my code:
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'tango_with_django_project.views.home', name='home'),
# url(r'^tango_with_django_project/', include('tango_with_django_project.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^rango/', include('rango.urls')), # ADD THIS NEW TUPLE!
)
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Rango says hello world!")
From the settings.py file
ROOT_URLCONF = 'tango_with_django_project.urls'
Hope you all can help get me started
Let's say I have a Django project called FailBook, with two apps, posts and links. If I look into FailBook/urls.py, I will find something like
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^posts/', include('posts.urls')), ## Custom url include
url(r'^links/', include('links.urls')), ## Custom url include
)
So then, when you look into the directory structure, you will notice that there are extra two urls.py files
FailBook
|-- posts
|-- models.py
|-- urls.py
|-- views.py
|-- etc.
|-- links
|-- models.py
|-- urls.py
|-- views.py
|-- etc.
# urls.py file in the posts folder
from django.conf.urls import patterns, include, url
from .views import PostListView, PostDetailView
urlpatterns = patterns('',
url(r'^posts/', PostListView.as_view()),
url(r'^posts/(?P<post_id>\d+)', PostDetailView.as_view()),
)
# where both views are class based views, hence the as_view function call
I know this was already solved, but the solutions provided did not help me. When I had this error it was as simple as checking all of the directories that should have had urls.py files.What I discovered was that the urls.py had not been added to the SVN repository that our Django app was pulled from.
I recommend looking in the projectname->projectname->urls.py for all references to app specific urls, and verifying that the urls.py file exists for each of them.
I had this issue while doing a Pluralsight Django tutorial. Two things I noticed:
1) I was using Django 2.0, and the command url() from Django 1.11 has been replaced with re_path() in 2.0, obtained by importing as follows:
from django.urls import path, include, re_path
This replaces,
from django.conf.urls import url, include
see this: Django 2.0 release notes
and,
2) I accidentally called the file to be imported from the subfolder, url.py, not urls.py
e.g. rango/url.py not rango/urls.py.
This was probably the main issue, and all flowed smoothly after that fix.