Django redirect "/" to index of installed app - python

I'trying to redirect the / of my domain to point to a index in my "frontend" app.
I tried a lot of ways and all of them work.
The problem is that my index_view is being called twice for every redirect.
Here is my top urls.py
urlpatterns = patterns('',
url(r'^$', lambda x: HttpResponseRedirect('/frontend/')),
url(r'^frontend/', include('frontend.urls', namespace="frontend")),
)
And here is my frontend/urls.py
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^alert/create/$', views.create_alert, name="create_alert"),
url(r'^alert/edit/(\w+)', views.edit_alert, name="edit_alert"),
)
Every time I go to / is calling my views.index twice and I can't see why =/
Am I doing the redirecting wrong ?
Thanks in advance for any help!

You can set the root to use your FE url patterns like this:
urlpatterns = patterns('',
url(r'^', include('frontend.urls', namespace="frontend")),
)
If you wanna forcibly redirect to /frontend/ then you will need a view to handle the redirect.
Maybe look at the Redirect Generic view: https://docs.djangoproject.com/en/1.1/ref/generic-views/#django-views-generic-simple-redirect-to

Related

Django Include() in urls.py with two apps

I believe this is a simple question but I am having a hard time figuring out why this is not working.
I have a django project and I've added a second app (sales). Prior to the second app, my urls.py simply routed everything to the first app (chart) with the following:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('chart.urls')),
]
and it worked fine.
I have read the docs over and over a looked at many tutorials, so my impression is that I can simply amend the urls.py to include:
urlpatterns = [
path('admin/', admin.site.urls),
path('sales/', include('sales.urls')),
path('', include('chart.urls')),
]
and it should first look for a url with sales/ and if it finds that then it should route it to sales.urls, and if it doesn't find that then move on and route it to chart.urls. However, when I load this and type in 127.0.0.1:8000/sales, it returns the following error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/sales/
Raised by: chart.views.index
which tells me that it is not routing my sales/ url to sales.urls but to chart.urls. When I change path('', include('chart.urls')), to path('', include('sales.urls')), it does route it to sales.urls so I know that my sales.urls file is set up correctly.
I know this is probably an easy question but I cannot figure it out with anything I've read. Any help is appreciated.
chart.urls:
from django.urls import path, re_path
from . import views
urlpatterns = [
path('dashboard/', views.chart, name='dashboard'),
path('', views.index, name='index', kwargs={'pagename': ''}),
path('<str:pagename>/', views.index, name='index'),
]
sales.urls:
from django.urls import path
from . import views
urlpatterns = [
path('sales/', views.sales, name='Sales Dashboard'),
]
I think the correct url for the sales page in your case would be http://127.0.0.1:8000/sales/sales/.
This is because sales/ is then looking in the included sales.urls and not finding any urls at the root / (the only url included there is at sales/ (within sales/) so will be sales/sales/
Reply to an OLD question but may be useful for others...
from django.urls import path, include
dont for get to import include
Do this:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('chart.urls')),
url(r'^sales/', include('sales.urls')),
]
you are not doing it properly since you are not telling your app where go once it is loaded .this r'^' to go directly to chart.url url(r'^', include('chart.urls')),

Django 1.9 web page doesn't redirect or change

I checked other pages to figure it out and I looked URL dispatcher but couldn't find a solution to this.
When I click to change the page from navbar nothing happens. homepage is extends to the header.html but others are not. infact when I click to another button change the page it stays still. here are my codes;
home/first/views.py
from django.shortcuts import render
def index(request):
return render(request, 'first/home.html')
def contact(request):
return render(request, 'first/iletisim.html', {'content':['Eger bizimle iletisime gecmek isterseniz mail adresimizi kullanabilirsiniz.', 'gulumhali#outlook.com']})
def home (request):
return render(request, 'first/home.html')
home/first/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^', views.index, name = 'index'),
url(r'^home/', views.index, name = 'home'),
url(r'^contact/', views.contact, name = 'iletisim'),
]
main urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('first.urls')),
url(r'^blog/', include('blog.urls')),
]
SOLUTION:
Like Adam said below; if you having this problem change the order home/first/urls.py for views.index.
Your first URL is catching everything. Essentially it's saying if the URL has a start to it, send to index.
Since it's the first url in the list, it's always going to catch that first since it goes through the url patterns and finds the first one it matches. As you've found out in the comment, if you move it to the bottom, when you go to either home/ or contact/ it will catch those first.
However, you still will have a problem with that because if you go to asdfblahblah/ or anything, it's still going to catch that. What you should do is add a $ to show that there should be nothing in the URL to route to index. That regex says if there is a start, and an end, and nothing in between, route to index.
urlpatterns = [
url(r'^$', views.index, name = 'index'),
url(r'^home/', views.index, name = 'home'),
url(r'^contact/', views.contact, name = 'iletisim'),
]

Django "homepage" app urls.py issue

I am pretty new to Django and when I laid my site out I did it in the following way:
The project is a "portal" of sorts,
App 1 "home" is the app that houses the homepage, login, registration
and other "site-wide" functionality
App 2 "inventory" is a basic asset inventory
App 3 "dashboard" is a set of status dashboards based on assets in the inventory
Right now I am trying to add the login functionality and I have a main project urls.py that looks like this:
File: /portal/urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('home.urls'), name='home'),
url(r'^admin/', include(admin.site.urls), name='admin'),
url(r'^inventory/', include('inventory.urls'), name='inventory'),
url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
url(r'^capacity/', include('capacity.urls'), name='capacity'),
)
My plan is to use the inclusion of .../home/urls.py to manage any site-wide functionality such as logging in, registering, etc.
Right now the home index view shows just fine, but anything else in .../home/urls.py gives me a 404
File: /home/urls.py
from django.conf.urls import patterns, url
from home import views
urlpatterns = patterns('',
url(r'^test/$', views.index, name='home_test'),
url(r'^ajax_login/$', views.ajax_login, name='ajax_login'),
url(r'^$', views.index, name='home_index'),
)
At this point I suppose my question is two-fold: Am I approaching this correctly? If so, how can I get the desired functionality? If not, how should I change the way things are set up/laid out to do it the correct "best practices" way?
Thanks in advance!
EDIT
Got it working thanks to dt0xff and holdenweb, accepted holdenweb's answer since it was more accurate including the need to put the home url inclusion below the rest.
Here is my new portal/urls.py file for reference
File: /portal/urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls), name='admin'),
url(r'^inventory/', include('inventory.urls'), name='inventory'),
url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
url(r'^capacity/', include('capacity.urls'), name='capacity'),
url(r'^', include('home.urls'), name='home'),
)
The issue is with your first pattern, which will only match the empty URL. So any empty URL will cause the home/urls.py URLs to be analyzed, but only the empty one will match even in that.
Unfortunately if there is no common prefix then that pattern "^" will match every URL (since they all start at the beginning ...).
So you should either use a common prefix for all the pages, or put the home URL specification at the end to give the other URLs a chance to match before it is tested.
Django looks in urls like this:
Get list of root urls - portal/urls
Find first, which matches to current url
If it is include, go to included urls, cutting off matched part
Your problem is here
url(r'^$', include('home.urls'), name='home'),
I mean, here
'^$'
You want url to match "start and then end of url". It works good with root(dunno.com/), but not with others, because url will contain something more...
So, just remove $ and you are fine
url(r'^', include('home.urls'), name='home'),
you never need to add regex($) at project/urls.py

Django get reverse url into a variable

I want a reverse url into a variable. I have to apps. one account and another profiles.
my main url
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^account/', include('account.urls', namespace="account")),
url(r'^profiles/', include('profiles.urls', namespace="profiles")),
)
my profiles app url
urlpatterns = patterns('',
url(r'^view/(?P<username>\w+)/$', views.profile, name="profile"),
)
my account app url
urlpatterns = patterns('',
url(r'^message/add_new/$', views.new_message, name='new_message'),
)
my account app view
def new_message(request):
if request.user.is_authenticated():
sender_username = request.user.username
sender_url = reverse('prfiles.views.profile', args=(sender_username,))
but I got 'NoReverseMatch at' error. Please suggest me a better way to solve this.
IMHO, prfiles.views.profile seems like typo.
Fix typo (if it is) or use the URL pattern name instead of the view name.
sender_url = reverse('profiles:profile', args=(sender_username,))
See Reversing namespaced URLs | Django documentation.

how can send blog.urls to my main page

I have a blog application and application has a urls.py which includes generic view
urlpatterns = patterns("",
url(r'^', ListView.as_view(
queryset=Post.objects.all().order_by("-created")[:2],
template_name='index.html')),
)
I send 2 data.
In main project urls.py like that:
urlpatterns = patterns("",
url(r"^$", direct_to_template, {"template": "index.html"}, name="home"),
url(r"^admin/", include(admin.site.urls)),
url(r"^blog/", include('blog.urls')),
)
I can see retrieved datas from db 127.0.0.1/blog/ I want to also see in 127.0.0.1 I mean in the start page.
I add this:
url(r"^$", include('blog.urls), direct_to_template, {"template": "index.html"}, name="home"),
But does not work. How can I achieve this?
This should Work, seems you have missed the closing of single quotes on your urls.py.I am using this and it works fine.
(r'^$',process.internal.views.HomePage.as_view(template_name='homepage.html')),

Categories

Resources