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
Related
Here is the screenshots of error i am getting.
and
App Name is greeting_app
Now greeting_app/urls.py has the following code
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('about/<int:id>/', views.about, name="about"),
]
greeting_app/views.py has the following code
from django.shortcuts import render, HttpResponse
from .models import basicinformation
# Create your views here.
def home(request):
information = basicinformation.objects.all()
return render(request, 'index.html', {"info":information})
def about(request, id):
data = basicinformation.objects.get(id=id)
return render(request, 'about.html', {"data":data})
templates/index.html has the following code. I have included only the url part in index.html file.
Description
data.id does not contain an ID. From the error message it contains ('',) (or possibly (",) - it's hard to tell from an image. Please don't use images!).
You do for all in info, then reference all in every other template line, then suddenly try and use data.id.
Since you haven't posted your whole template, it's hard to tell, but I suspect you want to use all.id in your URL here, and not data.id.
Add this to urls.py above urlpatterns
app_name=“ greeting_app”
Fix template url to this:
<a href="{% url "greeting_app:about" data.id %}">Description
New to Django and im trying to simply get to my products detail page using the following code in my URLs.py:
urls.py:
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from products import views
import carts
from carts.views import CartView
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('products/', views.all, name='products'),
path('s/', views.search, name='search'),
path('products/<slug:slug>', views.single, name='single_product'), <----This is the page I want to get to
path('cart/', carts.views.CartView, name='cart'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
If its any help, this is the link I use in my template to get to the product display page View
And here is my view:
def single(request, slug):
try:
product = Product.objects.get(slug=slug)
images = ProductImage.objects.filter(product=product)
context = {'product': product, "images": images}
template = 'products/single.html'
return render(request, template, context)
except:
raise Http404
But when trying to access this page I get the following 404 Error:
Maybe a fresh pair of eyes can help me spot the mistake I'm making?
If any additional info is needed please let me know, I'll be happy to provide. Thanks a million in advance!
First try replacing:
path('products/<slug:slug>', views.single, name='single_product'),
with
path('products/<slug:slug>/', views.single, name='single_product'),
Django automatically appends a slash (/) to the end of urls by default and the image you showed us shows an url ending on a slash while your defined path does not contain one. I just tried to reproduce it, and this made it work.
And then if it still doesn't work, double check if you are using the correct slug to find your Product using the shell as you are raising a 404 whenever an exception occurs. You might even consider removing the try/except to check if it's actually raising a different exception.
Maybe the last slash ('/') is the problem, try the url without it:
http://127.0.0.1/products/product-one
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.
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