I am facing some problem while mapping a view with URL
Project name-Hero
app name-Movie
Hero/url.py:
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns=[
url(r'^admin/',admin.site.urls),
url(r'^',include('Movie.urls'))
]
Movie/urls.py:
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^$',views.index,name='Movie'),
]
views.py:
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Hello friends ,Welcome to Django Project</h1>")
These are my urls and views configurations.I am getting a 404 page.
Please help me out.. Thanks
Try with below code, only changes in Hero/url.py
Hero/url.py
from django.conf.urls import include,url
from django.contrib import admin
urlpatterns=[
url(r'^admin/',admin.site.urls),
url(r'',include('Movie.urls')) # Do not add caret sign if further urls contains caret.
]
Related
Below is my code. In my hello_world project there is two app pages. one is home page and another is profile page. home page is working fine, but profile page is showing error.
hello_world urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('home_page.urls',)),
path('profile_page',include('profile_page.urls',))
]
home page urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.home,name='home page'),
]
home page views.py
from django.http import HttpResponse
def home(request):
return HttpResponse('home page')
profile page urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('profile_page',views.profile,name='profile page'),
]
profile page views.py
from django.http import HttpResponse
def profile(request):
return HttpResponse('profile page')
You need to use redirect method and include view name space as an argument..
Find the updated code:
from django.http import HttpResponse
def profile(request):
return redirect('profile page')
Don't forgot to import redirect...
The first argument of the path() function, i.e. the 'route' argument, must end with a forward slash.
path('profile_page/',include('profile_page.urls',))
Notice the forward slash at the end of the first argument, 'profile_page/'.
Your URLconf is not matching the expected pattern because of the missing slash.
Project name : CRMPROJECT
App name: leads
I'm working on a CRM project With Django and I keep getting this error while trying to map my urls to views
django.core.exceptions.ImproperlyConfigured. The included URLconf does not appear to have any patterns in it
Below is the project/urls.py file
'''
from django.contrib import admin
from django.urls import path,include
from leads import views
urlpatterns = [
path('admin/', admin.site.urls),
path('leads/',include('leads.urls',namespace='leads')),
]
'''
Below is leads/urls.py
from django.urls import path
from . import views
app_name = 'leads'
urlpatterns = [
path('',views.home,name='home'),
]
And lastly my leads/views.py
'''
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return render(request,'leads/base.html')
'''
I have created a project named firstproject and added a new project firstapp in which urls.py of firstapp is invoked to call view.py which displays a string in webpage. But the webpage is showing error.
I have created a project named firstproject and added a new project firstapp in which urls.py of firstapp is invoked to call view.py which displays a string in webpage. But the webpage is showing error.
firstapp
urls.py
from django.conf.urls import url
from . import views
urlpatterns={
url('r^$',views.index,name='index')
}
views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("<H2> Hey Everyone welcome to Django Tutorial! </H2>")
# Create your views here.
firstproject
urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$/', include('firstapp.urls')),
]
I expect the output to be "Hey Everyone welcome to Django Tutorial!"
In your firstapp urls.py there is a mistake use [] instead of {}
second mistake in is your code url regex ('r^$') is wrong
change it to (r'^$')
in firstapp/urls.py
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^$',views.index,name='index')
]
I am making my first django practice project I have a view called PostListview :
from django.shortcuts import render
from django.utils import timezone
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required
from blog.models import Post, Comment
from blog.forms import PostForm,CommentForm
from django.urls import reverse_lazy
from django.views.generic import(TemplateView,ListView,DetailView,CreateView,UpdateView,
DeleteView)
class AboutView(TemplateView):
template_name = 'about.html'
class PostListView(ListView):
model = Post
def get_queryset(self):
return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
I have this in in the urls:
from django.conf.urls import url
from blog import views
urlpatterns = [
url(r'^$',views.PostListView.as_view(), name= 'post_list'),
And this is the template which is calling this view.
<li class="navbar-brand bigbrand" >My Tech Blog</li>
This view is also set as the default home view and it opens fine at the time i open the site url (localhost) but when I am clicking "My Tech blog " it gives a 404 error.
This is the main urls.py :
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from django.contrib.auth import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',include('blog.urls')),
url(r'accounts/login/$',views.login, name = 'login'),
url(r'accounts/logout/$',views.logout, name = 'logout',kwargs={'next_page':'/'}),
]
Just change
url(r'^$',views.PostListView.as_view(), name= 'post_list'),
with
url(r'',views.PostListView.as_view(), name= 'post_list'),
I have the following code in my urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^testModule/',include(testModule.urls)),
url(r'^admin/', admin.site.urls),
]
and testModule is my app name which includes :
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index,name='index'),
]
And my views.py is
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello")
# Create your views here.
However, i get the following error while running the server:
line 20: url(r'^testModule/',include(testModule.urls)),
NameError: name 'testModule' is not defined
You haven't imported testModule in your main urls.
Had the same problem but solved it like this:
add "import testModule" to your urls.py
add "from django.conf.urls import url" to your urls.py in the app directory, in this case testModule
Once done you will get a "Page not found at/" error when you reload 127.0.0.1:8000. This is bacause the url to your app is actually at 127.0.0.1:8000/testModule