I'm just barely getting started with Python and Django.
I've created my modules, migrated, added my modules to the admin section, etc. Now, I need to create my first view. I've been following this guide (Yes the machine I'm on is running 1.6)
My views.py file in the app (setup) looks like this...
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Setup Index")
My app'a urls.py looks like this...
from django.conf.urls import patterns, url
from setup import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
And my root urls.py looks like this...
from django.views.generic import TemplateView
from django.views.generic.base import RedirectView
from django.conf.urls import include, patterns, url
from django.contrib import admin as djangoAdmin
from admin.controller import ReportWizard
from admin.forms import reportDetailsForm, reportDataViewsForm
from setup import views
djangoAdmin.autodiscover()
urlpatterns = patterns('',
url(r'^django-admin/', include(djangoAdmin.site.urls)),
#New User Wizard URL
url(r'^setup/', include('setup.urls')),
)
As far as I can tell I have followed the guide to the letter, but I am recieving a 404 error when navigating to myIP/setup.
There are no error messages on the 404 page. What is the issue?
For some reason your settings seem to override the default for either APPEND_SLASH or MIDDLEWARE_CLASSES.
Opening http://example.com/setup should cause a redirect to http://example.com/setup/, but somehow it doesn't for you.
Note that the latter URL is matched by your urls.py while the former is not (as should be the case).
The above should work if 'CommonMiddleWareis enabled andAPPEND_SLASHis set toTrue`.
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
Hello StackOverflow community,
I'm currently learning how to use the library Django combined with Python. However, I've ran into some issues which are somehow strange. My situation is the following. I have a project called "animals" which is the base of the Django application. My app is called "polls". Then I've defined the following views.
polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def animals(request, animal_id):
return HttpResponse("%s" % animal_id)
def index(request):
return HttpResponse('Hello world')
So far, so good. Unfortunatly I can't say the same about the urlpatterns.
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.index,name='index'),
# Redirects to localhost:8000/polls/<animal_id>
path('<int:animal_id>/',views.animals,name='animals')
]
Whenever I want to navigate to the url "localhost:8000/polls/10/" Django reminds me of the fact, that the url is not accepted by the application and a 404 Error is thrown inside my browser. Am I missing something here?
UPDATE
I've managed to resolve the problem by fixing a rather trivial error. While the polls/urls.py was alright, the problem lay inside the animals/urls.py file. This file looked like this:
animals/urls.py
from django.conf.urls import url
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
url(r'^admin/', admin.site.urls),
path('polls',include('polls.urls')),
]
As one can see, the "polls" path is not finished with a "/" sign. This indicates to Django that my desired route would be localhost:8000/polls where is any integer I add to the url. However, you have to add the slash at the end. Otherwise Django won't work as expected.
I migrated my Django project to an Ubuntu distant server. I'm using mod_wsgi in order to runserver and I have some questions about default page.
I'm really new in this domain and I apologize if my question is bad or useless ..
When I want to connect to my Django application, I have to write something like that :
http://172.XX.XX.XXX/Home/login/
If I write just :
http://172.XX.XX.XXX
I get :
Page not found
Using the URLconf defined in Etat_civil.urls, Django tried these URL patterns, in this order:
^admin/
^BirthCertificate/
^Identity/
^Accueil/
^Home/
^captcha/
^Mairie/
The current URL, , didn't match any of these.
My question is :
How I can define redirected url in order to write http://172.XX.XX.XXX in my browser and go directly to http://172.XX.XX.XXX/Home/login/ for example ?
This is urls.py file from my project :
from django.conf.urls import url, include
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
from BirthCertificate import views
from Identity import views
from Accueil import views
from log import views
from Mairie import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^BirthCertificate/', include('BirthCertificate.urls')),
url(r'^Identity/', include('Identity.urls')),
url(r'^Accueil/', include('Accueil.urls')),
url(r'^Home/', include('log.urls')),
url(r'^captcha/', include('captcha.urls')),
url(r'^Mairie/', include('Mairie.urls')),
]
Because I just want for example, write my IP adress in order to access to my Django application and not write all the time a complete url.
If you need some files (apache2 files, ...) please tell me which one I have to post there.
Include this in views.py file of your any app:
from django.shortcuts import redirect
def some_view(request):
return redirect('/Home/login/')
Suppose the view is in log app then,
Include this in your urls.py:
from log.views import some_view
urlpatterns = [
url(r'^$', some_view,name='index'),
url(r'^admin/', admin.site.urls),
url(r'^BirthCertificate/', include('BirthCertificate.urls')),
url(r'^Identity/', include('Identity.urls')),
url(r'^Accueil/', include('Accueil.urls')),
url(r'^Home/', include('log.urls')),
url(r'^captcha/', include('captcha.urls')),
url(r'^Mairie/', include('Mairie.urls')),
]
One method is to use RedirectView by making the following changes to your urls.py:
from django.views.generic.base import RedirectView
urlpatterns = [
url(r'^$', RedirectView.as_view(url='/Home'), name='home'),
...
]
You can either do this in Django or Configure from your webserver. Django has redirects app go through https://docs.djangoproject.com/en/1.10/ref/contrib/redirects/ for more info. You just add the source and redirect urls in the redirects section of your django admin.
Currently getting a 404 error for my application when I try to access the url /contato, and I am not sure why, can anyone help me?
Here is the error:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8002/contato
(...)
^contato/
(...)
Here is my code.
My main urls.py has:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/password_reset/$', 'django.contrib.auth.views.password_reset', name='admin_password_reset'),
url(r'^admin/password_reset/done/$', 'django.contrib.auth.views.password_reset_done'),
url(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm'),
url(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'),
url(r'^admin/', include(admin.site.urls)),
url(r'^perfil/', include('perfil.urls')),
url(r'^contato/', include('contato.urls')),
url(r'^$', 'views.index', name='index'),)
My contato.urls.py:
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('contato.views',
url(r'^contato/$', 'contato', name="contato"),
)
my views.py in contato:
from django.shortcuts import render_to_response
from django.template import RequestContext
def contato(request):
render_to_response('contato/contato.html',locals(),context_instance=RequestContext(request),)
You don't have a match for the url "/contato". In your base urls.py, you point the prefix "/contato" to include contato.urls, and then in that file you have a single URL which is again "/contato": so the combined URL for that view is "/contato/contato".
If you just want the URL to match "contato", you should either get the included url to match just "^$", or (probably better) don't bother including the separate urls.py and match the view directly from the base file.
I haven't found a satisfactory way of doing this: I have a djangocms setup that is working fine. But I need to add content from a table outside the CMS to my homepage and render that content on the template. I can do this, but editing the urls.py within CMS to use my views like so...
url(r'^', 'myapp.views.slideshow_info'),
... excludes any content from CMS. I understand that I just get my custom views to accommodate what CMS' views is doing, but how do I achieve this?
at the moment my app's views says:
from myapp.models import model1, model2
def slideshow_info(request):
return render_to_response('index.html', {'slideshow_list' : model1.objects.all()})
Many thanks
You can hook a custom app instance to any Django-CMS page. Here's the documentation on how to do so: http://docs.django-cms.org/en/2.1.3/extending_cms/app_integration.html#app-hooks You shouldn't need to alter the base url patterns to specifically re-route / to your view.
Before custom app-hooks were available, I would accomplish what you're trying to do with template tags.
Hope that helps you out.
Followup
Ok, in a recently completed site, I had to hook an app titled "portfolio" to display images on the home page of a Django-CMS site.
Here are the relevant portions of the code:
#portfolio/cms_app.py
from django.utils.translation import ugettext_lazy as _
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
class PortfolioAppHook(CMSApp):
name = _('Portfolio')
urls = ['portfolio.urls']
apphook_pool.register(PortfolioAppHook)
#portfolio/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('portfolio.views',
url(r'^(?P<slug>[-\w]+)/$', 'project_detail', name='project_detail'),
url(r'^$', 'portfolio_index', name='portfolio_index'),
)
#portfolio/views.py
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, render
from portfolio.models import Project
def portfolio_index(request):
project_objects = Project.for_public if request.user.is_anonymous() \
else Project.objects
projects = project_objects.all().select_related(depth=1)
return render('portfolio/index.html',
{'projects' : projects}, request)
def project_detail(request, slug):
project = get_object_or_404(Project, slug=slug)
if not project.public and request.user.is_anonymous():
return HttpResponseRedirect('/?login=true')
return render('portfolio/project_detail.html',
{'project' : project}, request)
#urls.py (base urls)
from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib import admin
from views import login_user, logout_user
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/filebrowser/', include('filebrowser.urls')),
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
url(r'^login/$', login_user, name='login_user'),
url(r'^logout/$', logout_user, name='logout_user'),
(r'^', include('sorl.thumbnail.urls')),
(r'^', include('cms.urls')),
)
if settings.SERVE_STATIC_MEDIA:
urlpatterns += patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
As you can see from this working example, I haven't altered my base URLs to accommodate the home page view, rather I've provided the URLs for my Portfolio app to Django-CMS through cms_app.py
Hope that gets you going.