Django. Confusion about redirections - django

Here is my simplified code:
project's urls:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^to_app1/', include('app1.urls')),
url(r'^to_app2/', include('app2.urls')),
)
app1's urls:
from django.conf.urls import *
from app1 import views
urlpatterns = patterns('',
url(r'^$', views.app1Page, name='app1-page'),
url(r'^action/', views.app1_to_app2, name='app1-to-app2-page'),
)
app2's urls:
from django.conf.urls import *
from app2 import views
urlpatterns = patterns('',
url(r'^$', views.app2Page, name='app2-page'),
)
app1's view:
def app1Page(request):
return render(request, 'app1/app1page.html')
def app1_to_app2(request):
# some actions going on here #
return HttpResponseRedirect('/to_app2/')
app2's view:
def app2Page(request):
return render(request, 'app2/app2page.html')
So, I have a button on app1page.html which should sends data through POST function to to_app1/action/ url. In the end it should redirect me to app2page.html, but it does not. It gives me GET 127.0.0.1:8000/to_app2/ HTTP/1.0 200 OK, but I staying on the same page (app1page.html).
There is obviously something I missing, could someone point out it to me?

Related

django 500 error coming instead of 404 for custom error page

here is my views.py
def handler_404(request):
return render(request, '404.html', status=404)
def handler_500(request):
return render(request, '500.html', status=404)
here is my urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from finish.views import handler_404,handler_500
from django.conf.urls import (
handler400,
handler403,
handler404,
handler500
)
urlpatterns = [
path('', include("scrap.urls")),
path('', include("details.urls")),
path('', include("finalize.urls")),
path('', include("finish.urls")),
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
handler404 = handler_404
handler500 = handler_500
when i am typing some url in which is not exist
it is coming "Server Error(500)" instead of coming "page not found(404)" error.
Please have a look into this....
Old question, but ill rederect you to Nico's answer. I spent too mutch time finding that.
Im running django 3.0.2 and to sum up i did this:
prodject - urls.py
from django.conf.urls import url, include, handler404
import app.views
urlpatterns = [
.
.
.
]
handler404 = app.views.error_404
app - views.py
.
.
.
def error_404(request, exception):
return render(request,'app/404.html', status = 404)
After reading many tutorials, I found out that most of them ommits the exception
Then put a 404.html in your app/templates/app folder
Hope someone finds this usefull..

Using the URLconf defined in tango_with_django_project.urls, Django tried these URL patterns, in this order:

I've been going through the Tango With Django 1.7 guide and I've come up with the following issue that I'm having troubles fixing:
Using the URLconf defined in tango_with_django_project.urls, Django
tried these URL patterns, in this order:
1. ^$ [name='index']
2. ^about/$ [name='about']
3. ^category/(?P<category_name_slug>[\w\-]+)/$ [name='category']
4. ^admin/
5. ^rango/$
6. ^about/
7. ^category/
8. ^media/(?P<path>.*)
The current URL, rango/category/python, didn't match any of these.
This is my rango/urls.py file
from django.conf.urls import patterns, url
from rango import views
urlpatterns = patterns(' ',
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
url(r'^category/(?P<category_name_slug>[\w\-]+)/$',
views.category, name='category')
)
In tango_with_django_project/urls.py I have
from django.conf.urls import patterns, include, url
from django.contrib import admin
from rango import views
from django.conf import settings
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
url(r'^admin/', include(admin.site.urls)),
url(r'^rango/$', include('rango.urls')),
url(r'^about/', include('rango.urls')),
)
if settings.DEBUG:
urlpatterns+= patterns(
'django.views.static',
(r'^media/(?P<path>.*)',
'serve',
{'document_root': settings.MEDIA_ROOT}),
)
In rango/views.py I have:
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render
from rango.models import Category
from rango.models import Page
def index(request):
category_list = Category.objects.order_by('-likes')[:5]
context_dict = {'categories': category_list}
return render(request, 'rango/index.html', context_dict)
def about(request):
return HttpResponse("Rango says: Hello world! <br/> <a
href='/rango/about'>About</a>")
def category(request, category_name_slug):
context_dict = {}
try:
category = Category.objects.get(slug=category_name_slug)
context_dict['category_name'] = category.name
pages = Page.objects.filter(category=category)
context_dict['pages'] = pages
context_dict['category'] = category
except Category.DoesNotExist:
return render(request, 'rango/category.html', context_dict)
I've been following the guide so I'm not quite sure what the problem is; I had a similar problem earlier on in the guide when I was getting the same error but with /about (never got this fixed).
Any idea what might be causing this problem? Any help is appreciated!
As already commented, the problem here is the definition of the URLconf, more precisely the inclusion of another URLconf that shall define every url that starts with rango/.... The crucial line is this one:
url(r'^rango/$', include('rango.urls')),
which should read instead
url(r'^rango/', include('rango.urls')),
The regex '^rango/$' captures: <start-of-string>rango/<end-of-string>
The regex '^rango/' captures: <start-of-string>rango/<whatever follows>, and that's where your included urls kick in.

Django Page not found (404) error with url config

I have a problem with urls config in my Django project, in my root url config i have this:
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('main.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^profile/', include('accounts.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
And in my app url config i have this:
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^$', 'main.view.home', name='main_home'),
url(r'^integrations/$', 'main.view.integrations', name='main_integrations'),
)
But when I go to render this url http://127.0.0.1:8000/integrations/ I obtain an error 404 Not Found.
Thanks by yours answers.
Yes, my app named 'main' and this is my view code:
def home(request):
user = request.user
if not user.is_authenticated():
data = {
'messages': get_messages(request),
}
return render_to_response('home.html', data, context_instance=RequestContext(request))
data = {
'messages': get_messages(request),
}
return render_to_response('home_user.html', data, context_instance=RequestContext(request))
def integrations(request):
return render_to_response('/main/integrations.html', context_instance=RequestContext(request))

what's wrong with my urls.py in this example?

I just installed userena, and had the example working from the tutorial, but as soon as I added in a single line in URLS.py, I'm getting an error. In the example below, I added the line mapping the home function from views.py
Now the issue I'm having is that when I go to 127.0.0.1/8000, I get TypeError: string is not callable, but then oddly, if I go to accounts/signup or accounts/signin, I am getting the template that should be appearing if i go to 127.0.0.1/8000.
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.views.generic import TemplateView
from accounts import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Here is my accounts/views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
def home(request):
return render('homepage.html')
You need to remove the quotes in the url and import that view
from accounts.views import home
urlpatterns = patterns('',
url(r"^$", home),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
You can steel use the strings in the url() but you must use the format 'app.views.viewname'
urlpatterns = patterns('',
url(r"^$", 'accounts.views.home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Or name the module in the first argument as string to patterns()
urlpatterns = patterns('accounts.views',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
the issue was I forgot to include the request in the return render.
The correct answer is that render is being called incorrectly. Actually, the views.py file would raise a SyntaxError, but we'll let that slide :)
# views.py
from django.shortcuts import render
def home(request):
return render(request, 'homepage.html')

django - invalid syntax (urls.py, line 7)

I'm doing a slight variation on my urls.py from the tutorial where I have the following -
mysite/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'^TidalDEV/', include('TidalDEV.urls')),
)
TidalDEV/urls.py -
from django.conf.urls import patterns, url
from TidalDEV import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
url(r'^(?P<pk>[0-9]+)/$', views.tesxml, name='tesxml'),
)
And this is the view in views.py -
def tesxml(self, request, pk, format=None, renderer_context=None):
"""
returns an XML of a jobmst listing
"""
template_vars['jobmst'] = (queryset1, [pk])
template_vars['jobdtl'] = (queryset2, [pk])
template_vars['jobdep'] = (queryset3, [pk])
t = loader.get_template('TidalAPI/templates/xml_template.xml')
c = Context(template_vars)
return HttpResponse(t.render(c), mimetype="text/xml")
When I try to hit my url at http://localhost:8080/TidalDEV/10081/ I get invalid syntax. What is the problem here?
Essentially I need the view to populate a template XML file I built.
You are missing a comma after your index view in TidalDEV/urls.py