Django Redirect URLs to absolute url - python

I'm new to Django/Python and I'd like to redirect some URLS to another domain.
mydomain.com/blog needs to redirect to 'http://blog.mydomain.com'
How can I go about doing this?
from django.conf.urls import patterns, include, url
from django.views.generic.base import TemplateView
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name='home'),
url(r'^blog/$', TemplateView.as_view(template_name='pages/blog.html')),
)

use the RedirectView generic view
url(r'^blog/$', RedirectView.as_view(url='http://blog.mydomain.com')),

Related

Prevent admin url from being overridden Django

I have a urls file that looks like this, however I cannot access the admin page. Is there any way to specify that the admin URL should never be overridden?
from django.contrib import admin
from django.urls import path
from latency import views
urlpatterns = [
path("admin/", admin.site.urls),
path("<str:chain>", views.regions),
path("<str:chain>/<str:region>", views.chain),
path("", views.index),
]```

Django, url not found?

I'm trying out Python Django. With eclipse pyDev. But I'm unable to simply get my first url to display.
This urls.py is from the Cr package.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index')
]
This urlspy is from the Crowd package.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'Crowd/', include('Cr.urls'))
]
So what I've understood from this the Crowd package is the "main" webservice(?), and by using include I can whenever the regular expression matches Crowd, will pass it on to the other urls.py(Cr). But the debugger passes:
Using the URLconf defined in Crowd.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, crowd, didn't match any of these.
my views.py file
from django.shortcuts import HttpResponse
def index(request):
return HttpResponse('<h1>Hello World!</h1>')
I tried to access it with http://127.0.0.1:8000/Crowd
Below is an image of the project folder.
Can we see your settings.py file? There is a place in there where you define your project's url file. I'm assuming it's right now either not there or it's pointing to the wrong place, because Django can't find your urls.py file.
For example, in my settings.py file for one of my projects, I have:
ROOT_URLCONF = 'Freya.urls'
Where "Freya" is my project name
Just for reference, not that I know this will solve your problem, this is what (part of) my urls.py file looks like for one of my projects:
from django.conf.urls import patterns, url
import views
urlpatterns = patterns(
'',
url(r'^$', views.index, name='index'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
)
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'Crowd/', include('Cr.urls'))
]
just use this urls.py file

Django - page not found 404

I'm trying to create a site using Django. I have the index view working, however I want to create a simple custom view and map to it but I am unable to. I'm getting a 404 error.
The app inside my project is called emails.
Here are the files:
base/urls.py
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'^emails/$', include('emails.urls')),
)
emails/urls.py
from django.conf.urls import patterns, include, url
from emails import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^custom/$', views.custom, name='custom'),
)
emails/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello world, you're at the index.")
def custom(request):
return HttpResponse("This is a custom view.")
Here is the 404:
Using the URLconf defined in crm.urls, Django tried these URL patterns, in this order:
^admin/
^/emails/$
The current URL, emails/custom, didn't match any of these.
When I visit localhost:8000/emails/ - I see the index view. However localhost:8000/emails/custom/ is returning a 404 error. Any help would be appreciated!
This url(r'^emails/$', include('emails.urls')), should be without the dollar sign $:
url(r'^emails/', include('emails.urls')),
You don't want to end your path after emails, so don't end the string with the regex character $. Let it be able to be continued in the other urls.py file

how to display a view in Django?

I'm totally new to Django, and I'm trying to understand how does it work (I'm more used to PHP and Spring frameworks.
I have a project called testrun and inside it an app called graphs, so my views.py looks like:
#!/usr/bin/python
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, World. You're at the graphs index.")
then, in graphs/urls.py:
from django.conf.urls import patterns, url, include
from graphs import views
urlpatterns = patterns(
url(r'^$', views.index, name='index'),
)
finally, at testrun/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'testrun.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^graphs/', include('graphs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
However, when I try to access http://127.0.0.1:8000/graphs/ I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/graphs/
Using the URLconf defined in testrun.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, graphs/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
What am I doing wrong that I can't get that simple message to be displayed in the browser?
To expand on my comment, the first argument to patterns() function is
a prefix to apply to each view function
You can find more information here:
https://docs.djangoproject.com/en/dev/topics/http/urls/#syntax-of-the-urlpatterns-variable
Therefore in graphs/urls.py you need to fix the patterns call like so:
urlpatterns = patterns('', # <-- note the `'',`
url(r'^$', views.index, name='index'),
)

Django url Routing Error

I have a very basic url router in my django project:
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
admin.autodiscover()
urlpatterns = staticfiles_urlpatterns()
urlpatterns += patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^/?', include('customApp.urls')),
)
When I start the dev server and go to 127.0.0.1:8000/admin/, I get a ViewDoesNotExist at /admin/ error.
Here is the content of the exception:
Could not import customApp.views.event. View does not exist in module customApp.views.
I've already tried re-ordering the urls (I have no idea how that would help, but I tried it anyway) and changing r'^/?' to r'^/'.
When I comment out the last url, the admin page works again.
Here's the customApp.urls code:
from django.conf.urls import patterns, include, url
import django.contrib.auth.views
import django.contrib.auth
urlpatterns = patterns('customApp.views',
url(r'^$', 'index'),
url(r'^rest/v1/event/add/$', 'event'),
url(r'^rest/v1/reports/$', 'reports'),
)
urlpatterns += patterns('',
url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
)
It is simple buddy. Django cannot find customApp views. Please ensure whatever views you have got in urls.py, it should exist.
Thanks

Categories