Django flaky url (randomly omits WSGIScriptAlias) - django

My Django site is producing URLs that intermittently omit my WSGIScriptAlias. If I simply print out {% url 'index' %} in my index.html (see my urls.py settings below) I randomly (around 50% of the time) get either:
MySiteAlias/MySite
which is correct, or
MySite/
which is incorrect.
myapp/urls.py:
from django.conf.urls import url,include
urlpatterns = [
url(r'^MySite/', include('mysite.urls')),
]
mysite/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
and views.index basically does return render(request, 'mysite/index.html). Any ideas on how to fix this?

I'd imagine you might have two urls with the same name, this is where namespaces will help. If you provide a namespace for your mysite.urls then there is no confusion on where you should go to
url(r'^MySite/', include('mysite.urls', namespace='mysite')),
{% url 'mysite:index' %}

Related

How to convert Template tags from normal django to the one django-hosts use

I have just added django-hosts to setup subdomains for my site which works perfectly. Next step is just to convert all the normal django URL's in my template to the one django-hosts like.
I know how to link pages , but once I need to add variables to my URL's i'm not sure how to construct the code for it.
Normal django URL that works
{% url 'golemstats:nodeinfo' node.Node_id node.Node|slugify %}
How do I convert that to a URL that django-hosts like? I've tried the following:
{% host_url 'nodeinfo' host 'golemstats' 'node.Node_id' 'node.Node|slugify' %}
hosts.py
from django.conf import settings
from django_hosts import patterns, host
host_patterns = patterns('',
host(r'www', settings.ROOT_URLCONF, name='www'),
host(r'golem', 'golemstats.urls', name='golemstats'),
)
golemstats.urls
from django.urls import path
from . import views
app_name = 'golemstats'
urlpatterns = [
path('', views.index, name='index'),
path('node', views.searchNode, name='searchNode'),
path('node/<nodeid>/<node>', views.nodeinfo, name="nodeinfo"),
path('version-notifier', views.notifierIndex, name="notifier"),
path('ports', views.portScanner, name="portscanner"),
path('scoreboard', views.scoreboard, name="scoreboard"),
path('tools', views.tools, name="tools"),
path('troubleshooting', views.troubleshooting, name="troubleshooting"),
path('network', views.networkOverview, name="networkOverview"),
]
Fixed. have to put arguments after host_url
{% host_url 'nodeinfo' node.Node_id node.Node|slugify host 'golemstats' %}

Django doesn't load second page

I have just started Django, and I'm facing some difficulty.
When the first time I'm loading "localhost:8000/first_app" it is successfully loading index(), but on clicking on "About" link, url is changing to "localhost:8000/first_app/about/", but it is still loading "index()" and not "about()". Don't know what I'm missing.
Here's my project's URL:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^first_app/', include('first_app.urls')),
)
App's URL:
from django.conf.urls import patterns, url
from first_app import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^about/', views.index, name='about'),
)
And views.py:
from django.http import HttpResponse
def index(request):
return HttpResponse("Rango says: Hello world! <br/> <a href='/first_app/about'>About</a>")
def about(request):
return HttpResponse("This is the ABOUT page! <br /> <a href='/first_app/'>Index</a>")
I'm using Django 1.7 and python 2.7.
Thanks.
You need to define your URLs like this;
urlpatterns = patterns('',
url(r'^about/$', views.about, name='about'),
url(r'^/$', views.index, name='index'),
)
Basically '^$' is the beginning & end of the match. The ^ is the start of the pattern & the $ is the end of the pattern so keep that in mind when defining your URLs. It's good practice to use $ to end your urls to avoid views being rendered regardless of what you add to the URL after what you match in your pattern.

Django URL in template NoReverseMatch

I keep getting a NoReverseMatch in my base template, though I've specified a namespace and have put the proper name.
Error:
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['forum/|^forums/$']
Main urls.py:
from django.conf.urls import url, include
from django.contrib import admin
from forum.views import main_home
urlpatterns = [
url(r'^$', main_home, name='home'),
url(r'^admin/', admin.site.urls),
url(r'^accounts/|^account/', include('accounts.urls',
namespace='accounts')),
url(r'^forum/|^forums/', include('forum.urls', namespace='forum')),
]
Forum urls.py:
from django.conf.urls import url
from forum import views
urlpatterns = [
url(r'^$', views.home, name='home'),
]
From my template:
<a class="nav-link" href="{% url 'forum:home' %}">Forum</a>
The docs for reverse() say You cannot reverse url patterns that contain alternative choices using |.
In this case, you could change the URL to:
url(r'^forums?/', include('forum.urls', namespace='forum')),
However, it might be better to choice a single URL /forums/ or /forum/. Having a single URL can be better for SEO.

how to design Django url pattern to avoid 301

I have a project called blog, the url pattern shows below. I want to make the app "posts" to route all the traffic.
Url patterns of them shows below:
#blog/urls.py
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('posts.url', namespace='posts')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
#posts/url.py
from django.conf.urls import url
from django.contrib import admin
from .views import (
home,
down,
get_data_by_post,
)
urlpatterns = [
url(r'^$', home, name='down'),
url(r'^api/$', ins_down, name='api'),
url(r'^p/(?P<slug>[-\w]+)/$', get_data_by_post, name='slug'),
]
When enter into home page, the home function in posts.views will generate some links with local data to render index.html.
def home(request):
final = get_local_suggest()
return render(request, "index.html", final)
Some of the code in index.html template likes below:
<a href="p/{{ results.code }}?From=homepage" class="portfolio-link" target="_blank">
So in home page , some links will show there: "http://example.com/p/code?From=homepage
But the tricky question here is that: when I click the url , the console of Django will print 301 like below. In browser, it will redirect from "/p/code" to "/p/code?From=homepage".
Not Found: /p/code [17/Apr/2017 15:05:23] "GET
/p/code?From=homepage HTTP/1.1" 301 0
There are must be something wrong with url pattern design, how to avoid it happened again?
Thanks!
Your url pattern ends with a slash, so your url should as well.
To make sure you always point your urls to the canonical url and avoid redirects, use the {% url %} template tag:
<a href="{% url 'posts:slug' results.code %}?From=homepage" class="portfolio-link" target="_blank">
Here 'slug' is the name of your url, and results.code is an argument to the url.
You can use request.GET.get('From', '') in your views, and clean your url pattern.

Django admin urlpatterns problems

I'm trying to complete a Django tutorial (https://docs.djangoproject.com/en/dev/intro/tutorial03/) and have run into a problem which, I think, is my understanding of regular expressions and the include() function.
My problematic 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'^polls/', include('polls.urls')),
)
which, leads me (upon browsing to http://localhost:8000/admin/) to the detail view of polls.urls - the code of which is:
from django.conf.urls import patterns, url
urlpatterns = patterns('polls.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
I get why it's matching to polls and getting taken through to polls.urls. Any ideas?
Note: when I comment out the url(r'^polls/, include('polls.urls')), line I can see the admin page again
Thanks!
O.K - so I managed to fix this but am not entirely sure how. It was a case of leaving it and working on other problems and when I came back it was fixed! So, things that I changed:
1.) included {% load url from future %} to ensure forward compatibility in my templates
2.) changed the form action in detail.html to <form action="/polls/{{ poll.id }}/vote/" method="post"> (from <form action="{% url 'polls.views.vote' poll.id %}" method="post">
3.) finally, and probably most importantly - sorted my syntax out! In my views.py file I had Return HttpResponseRedirect(reverse('polls_results', args=(p.id,))) and not Return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))
I still don't know which did it but, if anybody is in the same position as me, some combination of those 3 will do it!