Django 1.5 robots.txt - django

Here is what we have in our url conf:
from django.views.generic import TemplateView
.....
url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.html')),
For some reason, we're getting redirected to ?next=robots.txt when attempting to hit *.com/robots.txt
Is there something obvious we're missing ? This seems to work locally in vagrant environments but it's not working in production (redirecting through ?next=)
update 1
There is no obvious reason that this would be behind a #login_required decorator or any required login function...
update 2
Sure enough, you can see robots.txt when you are authenticated....
update 3 direct_to_template has been deprecated, I'm sure that used to work but apparently has been deprecated in django 1.5
update 4 seems like this post has a way to force login_required for a TemplateView.as_view: How to require login for Django Generic Views? but it seems in our case this is being force by default ?

Related

How to return a 404 for just Django allauth signup page?

I am rather new to Django and all of the work I have done so far has been with models/views/viewsets.. The site I am working on incorporates Django allauth for authentication. I have successfully edited/styled the login/logout templates, but the page will be accessed by people who are given credentials created in the admin section rather than signing up on their own- so the sign up page is unnecessary. I'd like to just show a 404 page anytime someone lands on the signup page. I have already removed all the links to the signup page from the other templates.
In short- how do I just redirect someone to the Django default page_not_found when they hit /accounts/signup/?
My attempts so far have revolved around editing the URLs.py file to include something like path('account_signup', page_not_found) (after importing it at the top), or some other manipulation of that line. I'm probably missing something really easy, as I have been getting a little frustrated... And I haven't found any stack overflows where someone was desiring a 404 when a user navigated to one of the allauth account pages.
In order to server 404 pages automatically for not found url, create a 404 view and then in main projects urls.py have below code
Read the Official docs
handler404 = 'mysite.views.my_custom_page_not_found_view'
For redirecting use Redirect View in django docs
from django.views.generic.base import RedirectView
url('/accounts/signup/', RedirectView.as_view(url='/', permanent=False),name='index')
Note their is 404 page for developers builts in django, but once you turn debug=False in settings for production apps,it is not visible,
You can simply not use the signup page in your urls.
On the other hand, it is bad practice to use createsuperuser to create users, since by default they will have enough privileges, even to log in to admin and edit things. The right thing to do is to create a user with some method you want, and with the permissions you give them.
This last one will allow you to use a decorator in your signup view that only allows access to that page in case you have an account with a particular privilege, and not any user. There is no point in returning a 404.

django project derived from python-socketio django example unable to server other URLs (at least in django 1.11)

After playing with python-socketios django_example, and seeing that it worked great, I created a new django project, configured it just like the example, copied over the example app to the project (complete with it's overriding of the runserver management command). Everything worked fine, and I was able to make my a few changes so that you can set a nick, some redis stuff to lookup up the sid for a nick, and was able to support sending private messages to a nick. Everything was still working great.
I figured the next logical step was to, rather than having to manually set a nick, require the user to login, expose their username as a var in a script block in the template (I moved scripts/index.html to templates/index.html), and automatically have the javascript emit my custom 'set_nick' event with the username automatically upon connect.
I defined LOGIN_URL = '/accounts/login' in settings.py, included 'django.contrib.auth.urls' in my urls.py and wrapped the index view with #login_required.
It was only then that I noticed that no matter what URL you request, you always get the chat apps index view - no login page redirect, '/admin/' is ignored, etc.
EDIT Solved - See my answer below.
I noticed that the urls.py which I blindly copied over from the example looked like this:
url(r'', include('socketio_app.urls')),
url(r'^admin/', admin.site.urls),
and the r'' was the culprit (matches everthing). Changing this to:
url(r'^/', include('socketio_app.urls)),
url(r'^admin/', admin.site.urls),
However, I'm using Django==1.11. I believe Django 2 tends to suggest using path (or some similarly named function) rather than using url. I don't believe, however, that the semantics of url are different in Django 2, so this is probably an issue for Django 2 users as well.

Django - Loading Robots.txt through generic views

I have uploaded robots.txt into my templates directory on my production server. I am using generic views;
from django.views.generic import TemplateView
(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
However, when I load robots.txt on the browser I get a 404 - Page not found.
Can someone suggest what needs to be done to fix this. Thanks.
I should point out that on the local environment this seems to be working.
Finally got it. I had to add a '/' in ^robots.txt$
(r'^robots\.txt/$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
That's elementary! I presumed that by default APPEND_SLASH it True however, on the production server it didn't work.
Let me know if anyone can provide some insights on it.

Django login_required redirect with hash [duplicate]

I am using django-braces's LoginRequiredMixin in a Django 1.6 project. This mixin replicates Django's login_required decorator.
I have a view that uses the LoginRequiredMixin that has a URL like this: /spa_home/#price_requests/68. If I try to hit this URL without being logged in, the mixin correctly sends me to the login page with a request like this: /accounts/login/?next=/spa_home/#price_requests/68. Unfortunately, after successfully logging in, the URL hash fragment is left off and I am just redirected to /spa_home/.
What is the best way to fix this? Removing hash fragments from my application would be a large effort.
The issue is the way the browser interprets the login URL. You want it to be intepreted like this:
/accounts/login/?next="/spa_home/#price_requests/68"
but actually, it is seen like this:
"/accounts/login/?next=/spa_home/"#price_requests/68
In other words, the hash is seen as attaching to the login URL itself, not the redirect parameter.
The way to fix this is to quote the parameter:
urllib.quote('/spa_home/#price_requests/68')
which gives you /spa_home/%23price_requests/68, which will be interpreted correctly.

Generating Login and Registration Forms with Contrib.Auth in Django

I'm new to Django and in bouncing around the framework over the past few days I haven't been able to figure out how to properly install the django.contrib.auth app in my project.
Well, installing is probably not the right word, but configuring it for my purposes.
What I'm truly hoping to do is extend the built-in classes to simply create registration and login forms, since my User class is working just fine from terminal.
In settings.py I have django.contrib.auth in my INSTALLED_APPS.
I also have installed the Authentication Middleware and Sessions Middleware.
I can also clearly see in Django.contrib.auth.views and Django.contrib.auth.forms where the registration and authentication handlers are.
My problem, it seems, since I'm new to the framework, is properly including these files in my project and generating the HTML forms for registration and login.
(As in, do I need to include these Auth files in my app's forms.py? What do I need to model that hasn't already been modeled for me? And finally, since I can see in Django.contrib.auth.views a need for a registration directory with HTML templates, how can I get these all communicating with each other properly?)
Figured out the problem. Just needed to follow Django's URL Conf conventions. Example: (r'^accounts/login/$', 'django.contrib.auth.views.login'),
James Bennet's django-registration is an excellent helper application used for a common registration / login pattern.