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')),
Related
I am learning Django. I build an app in which I am setting up the URL patterns but in all cases it's displaying the data for the index page.
appTwo urls.py File :
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^$', views.users, name='users'),
url(r'^$', views.help, name='help'),
]
MyProject urls.py file :
urlpatterns = [
url(r'^$', views.index, name='index'),
url('admin/', admin.site.urls),
url(r'^users/', include('appTwo.urls')),
url(r'^help/', include('appTwo.urls')),
]
If I call /users or /help, the browser display the data for index file only.
Is there something with a regex that I am doing wrong?
You're setting your urlpatterns in a wrong way. First of all, in you project's urls.py file you are saying that if a request is sent to /users or /help, then Django should look into your appTwo.urls. When Django gets there, it finds that the urlpatters are set so that everything that's empty after any of the aforementioned urls must be handled by the views.index, views.users and views.help. But as views.index is the first one in the list, then all the request end up being handled by that view.
By the way, if you're using Django >= 2.0 you no longer need to use the url() function, but the path() one instead, for which you can declare the paths as simple strings rather than regular expressions.
You should have something as follows:
MyProject/urls.py
from django.urls import path
urlpatterns = [
path('', include('appTwo.urls')),
path('admin/', admin.site.urls)
]
MyApp/urls.py
from django.urls import path
urlpatterns = [
path('', views.index, name='index'),
path('users/', views.users, name='users'),
path('help/', views.help, name='help'),
]
when django reaches here (in your apps url) while resolving the urls, see all three urls have empty regex as first param (so the first one will be selected), And as you see its index then you always show index page. You need to differentiate the urls. Something like this.
urlpatterns = [
url(r'^users/$', views.users, name='users'),
url(r'^help/$', views.help, name='help'),
url(r'^$', views.index, name='index'),
]
I think you also cant write url index with empty regex at top of other urls, otherwise it will always be selected.
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')),
I have a blog made in django in a VPS. The blog is working fine but to access it I have to write the url example.com/blog/
What I'm trying is to make an automatic redirection so when a user enters example.com/ it automatically redirects to example.com/blog/
The project is set under apache.
This is my the configuration in myproject/urls.py:
urlpatterns = [
url(r'^blog/', include('blog.urls', namespace="blog")),
url(r'^admin/', include(admin.site.urls)),
]
This is the configuration of myproject/blog/urls.py that right now is formed by a post list and a post detail:
urlpatterns = patterns('',
# Index
url(r'^(?P<page>\d+)?/?$', ListView.as_view(
model=Post,
paginate_by=5,
),
name='index'
),
# Individual posts
url(r'^(?P<pub_date__year>\d{4})/(?P<slug>[a-zA-Z0-9-]+)/?$', DetailView.as_view(
model=Post,
),
name='post'
),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I already tried to add a .htaccess with different configurations but it's not working.
Is there a way to redirect from django?
If you're sure that you want that / to redirect to /blog/ for that you can create a temporary index view and redirect the response to /blog/
url(r'^', temp_index, name='index'),
def temp_index(request):
return HttpResponseRedirect('/blog/')
If you want that / must show /blog then replace
url(r'^blog/', include('blog.urls', namespace="blog")), with
url(r'^', include('blog.urls', namespace="blog")),
I have created a small project like this structure..
|-mysite
|-polls
|---static
|-----polls
|-------css
|---------images
|-------images
|-------js
|---templates
|-----polls
|-templates
|---admin
Here In polls is the app and now i am getting output with this url http://127.0.0.1:8000/polls/
In main folder i.e mysite folder in urls.py i have code like this..
urlpatterns = patterns('',
url(r'^polls/',include('polls.urls',namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
Now in polls folder in urls.py code is..
urlpatterns = patterns('',
url(r'^$',views.index, name = 'index'),
}
Now i want to get the page like main website address like.. http://127.0.0.1:8000/
How to get?
Thanks.
Can you change what you have here
url(r'^polls/',include('polls.urls',namespace="polls")),
to
url(r'^', include('polls.urls', namespace="polls")),
Now the reason is this:
Django urls uses regex. In your example, you're telling django to catch your polls app, beginning from the url as localhost:8000/polls/
Learn more: https://docs.djangoproject.com/en/dev/topics/http/urls/
Even in your root project urls.py, you've got this:
# Examples:
# url(r'^$', 'Toolkit.views.home', name='home'), #this will match url of the type localhost:8000
# url(r'^blog/', include('blog.urls')), # this will match url of the type localhost:8000/blog/
Just look sharp!
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