I have a problem with my project & I hope that you will help me figure it out.The main problem is that my project is a one-single page template, so I don't have so many views , but I want to know , how to redirect to my homepage
from django.conf.urls import url
from django.contrib import admin
from iubestete import views as iubestete_views
urlpatterns = [
url(r'^$', iubestete_views.index),
url(r'^',iubestete_views.index,name="portal"),
url(r'^admin/', admin.site.urls),
]
here is my urls.py file , which function should I import or what should I do? I mean, I want to know for example, if a person types "http://127.0.0.1:8000/adsnjiwadi/" ,how can I redirect that link to my homepage(index.html)?Thank you so much & I hope that you'll have a great day:) Peace:)
Your code url(r'^',iubestete_views.index,name="portal") in urls.py already catch all URL pattern, it will direct to your home page.
eg: http://127.0.0.1:8000/adsnjiwadi, http://127.0.0.1:8000/sfddasfdfaf/ddfsaf, etc. will go to your home page (index).
In your urls.py
url(r'^(?P<garbage>.*)/$', views.garbage,name='redirect')
In your views.py
from django.core.urlresolvers import reverse
def garbage(request, garbage):
return HttpResponseRedirect(reverse('index'))
This is is a hacky method. Anything other than your root url will return to index url.
Related
I'm a newbie to Django and I know this probably has been asked alot of times.
So basically what's happening is when I try to create a new project and whenever I'm trying to run my server, by default it's opening http://127.0.0.1:8000/catalog/ and not http://127.0.0.1:8000/.
Even if I run the server with my other projects, I'm facing the same error.
I followed this django basics tutorial on https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website
Idk but somehow I think it's default address is set to http://127.0.0.1:8000/catalog/.
Here's the link to the repo for the project:
https://github.com/Fanceh/django-404-error
Here's my project's urls.py:
from django.contrib import admin
from django.urls import path, include
from testuapp import urls
urlpatterns = [
path('admin/', admin.site.urls),
path('',include("testuapp.urls"))
]
Here's the code in my testuapp urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.testu),
]
Here's my webapp's views.py file:
from django.shortcuts import render
# Create your views here.
def testu(request):
render(request, 'Greetings!')
Is there any way I can change it?
Regards
From the tutorial link it mentions the redirect.
Any request for the root URL, will redirect you to /catalog.
Screenshot from the tutorial below.
HTH
so i assume you are below url pattern structure in your testuapp project.
urlpatterns = [
path('catalog',include("views.catalog"))
]
views.calalog is the name of the method in your view file.
Ok I think I figured it out, it's just the chrome cache. I cleared it and bam it's working!
hey guys i have been learning Django for about 3 week now and i am just curious is there anyway to set custom url for main page(the first page shown when starting the server) because i try to do it like the code below but still it give me error if anyone know please help if not hmm...maybe that how Django operate i think haha. Thanks
// Django api
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('account/',include("useraccount.urls")),
]
//useraccount
from django.urls import path
from . import views
urlpatterns = [
path('login',views.login,name="login"),
]
if you want a url for home page you can do something like this in your main url
path('', HomeView.as_view(), name='home'),
I'm stuck with a Django project, I tried to add another app called login to make a login page but for some reason the page just redirects to the homepage except for the admin page
For example: 127.0.0.1:8000 will go to the homepage but 127.0.0.1:8000/login will also display the homepage even though I linked another template to it.
Here is my code:
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('portal.urls')),
url(r'^login/', include('login.urls')),
]
login urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^login/', views.index, name="login"),
]
login views.py
from django.shortcuts import render
def index(request):
return render(request, 'login/login.html')
portal urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^', views.index, name="portal"),
]
I see 2 problems here:
As #DanielRoseman mentioned above, the regular expression ^ matches anything, so you should change it to ^$.
When you use an include, the rest of the path after what the include matched is passed to the included pattern. You’ll want to use ^$ in your login urls.py too.
You don't terminate the portal index URL, so it matches everything. It should be:
url(r'^$', views.index, name="portal"),
In addition, if the regex is login/$ but you enter http ://server/login, then it won't match wheras http://server/login/ will.
You could try changing the regex to login/*$, which will match any number (even zero) / on the end of the url.
So http: //server/login, http: //server/login/, http: //server/login//// would all match.
Or if you want to be specific, login/{0,1}$ might work (though that regex syntax is from memory!)
I have my url:
url(r'^home/', HomeQuestionView, name='home_question') ,
When I enter localhost:8000/home I get my homepage but what I want is when I just enter I get my homepage.
I mean I want to redirect to the above homepage url when user enters only my site likewww.xyz.com not www.xyz.com/home
I dont want to configure in this way
url(r'^', HomeQuestionView, name='home_question') ,
Thanx in advance
Use generic RedirectView:
from django.views.generic.base import RedirectView
url(r'^$', RedirectView.as_view(url='/home/')),
I found this solution (long story short, you need to add views.py to your main project folder and make view which redirect your empty route to your url):
In your urls.py inside urlpatterns add this:
urlpatterns = [
...
path("", views.home, name="home"),
...
]
In main Django folder create views.py and import it in urls.py
example
In views.py add this code:
from django.shortcuts import render, redirect
def home(request):
return redirect("blog:index") # redirect to your page
Now every time your url will be empty like (localhost:8000) it will be redirected to (localhost:8000/your_adress). Also, you can use this not only with blank url, but anywhere you need this sort of redirection
The only examples I find. are related to issues with login page and iteration to other pages but not in the way I have the problem, so here is the issue I have to deal with -
I want to display a form for creating an account with multiple steps, using modals, when a user access the button "subscribe"
on my homepage.html I have this:
<a onClick="window.location.href='account'" target="_blank">
<input type="submit" value="account">
</a> `
...which is supposed to go to a new account.html page, in the same folder as my homepage.html
in my app's urls.py, where the apps' name is homepage I have:
from django.conf.urls import patterns, url
from homepage import views
urlpatterns = patterns('',
url(r'^$', views.homepage, name='homepage'),
url(r'^account$', views.account, name='account'),
)
and in my views I have:
from django.shortcuts import render
from homepage.models import Email
def tmp(request):
latest_email_list = Email.objects.order_by('-pub_date')[:0]
context = {'latest_email_list': latest_email_list}
return render(request, 'home_page/homepage.html', context)
def homepage(request):
return render(request, 'home_page/homepage.html')
def account(request):
return render(request, 'home_page/account.html')`
when I click on the button I get
Not Found
The requested URL /account was not found on this server.
I am a complete beginner in django and python so I really haven't yet wrapped my mind on how to work properly with the urls, views, and models together but I assume I have something wrongly defined in my views
would be grate if someone could help me setting this up,
Thanks
I only want to thank all those who took time to check my question and tried to give a solution.
I think it was my mistake that I did not post the code I have in my main urls.py file, so here it is:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^$', include('home_page.urls', namespace="homepage")),
url(r'^admin/', include(admin.site.urls)),
)
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
Apparently, the problem was in that first prefix of the first url in the list:
I changed
url(r'^$'
for
url(r''
and now it calls whatever links I provide in my html pages.
Thanks all again