django 1.4 caching GET to /login/ - django

I recently upgraded my django framework from 1.3 to 1.4. Today I ran some tests on the login page. I have a switch which determines whether a 'reset password' link should be displayed on the login screen. This test worked nicely under 1.3, but doesn't under 1.4.
I've setup my own view and template for the login page as follows:
urlpatterns = patterns('framework.views',
url(r'^$', 'index'),
url(r'^login/$', 'login_view'),
url(r'^logout/$', 'logout_view'),
...
Upon some further investigation I noticed that if I browse to the login page the first time, it works. If I then remove the entries from my urls.py file (ie the /login/ entries), I can still browse to the /login/ page. Even when I restart the django test server, that url entry is still valid. Even deleting the urls.pyc file doesn't give me a 404. Its only when I try and post, that I'll get a 404. Incidentally, this phenomenon doesn't happen for some of my other urls.
I have a feeling the reason why my tests fail is that django somehow caches the /login/ request in some mysterious way and so the login page never refreshes for each of the tests I run on the screen. Does anybody know how to overcome this problem and perhaps also why it is that this particular feature has changed. Does it have anything to do with the newly implemented template response??

Do you use firefox? Try to remove Firefox cache, or whatever browser's cache you are using...
I ran into the same issue yesterday. I look around and i found a lot of people having this issue. Have a look at this...
Don't blame django as i did in first place ;) (i blamed me later, before knowing the real problem)...
Let's blame the protocol :P
Hope this solves your problem!
EDIT:
Here you have some possible solutions to your problem (if you use firefox):
1) http://support.mozilla.org/es/questions/848678
2) https://superuser.com/questions/23134/how-to-turn-off-firefox-cache

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 session random timeout

I have a website, and i've been writing websites on django for a while now, but never encoutered something like this before...
Trouble is that when i login everything seems alright, but as surf random pages , pretty often happens thing, that code between {% if user.is_authenticated %} {%endif %} dissapears as i am not logged in , though i just was .
If i go back to previous page where i was logged, before going to next page , it shows i am logged in again , and it happens completely randomly. It's not like it happens after specific actions.
Often when i try to do it on purpose everything works fine , but as some time passes something like this occurs. Though if go to login page, while it randomly shows i am logged out, it automatically logs me back because like i understand session does exist, for some reason django just does not see it .....
I am very confused what might be wrong. Any possible advice would be great.
1) I am not using any session functions. Basically nothing that can on purpose trigger those events. Just simple logic in views and return render_to_response('template', RequestContext(request, {}))
2) Django is running on nginx and uwsgi
3) Here is the website site, which is currently under development, but you can login with user test, and password test to try ... maybe the same effect occurs and you'll see it.... but i do not guarentee that it will occur immidiately, it is completely random. To try u just have to click random pages..... and eventually u'll see that it shows that you logged out, though you didn't.
Your post does not give us much info to work with, and your page times out.
My best suggestion is to add the login_required decorator to all of your views. If you are in fact being logged out you will be able to see where this occurs.
Add the import to the top of your page, and the decorator above every view like so:
from django.contrib.auth.decorators import login_required
#login_required(login_url='LINK_TO_LOGIN_URL')
def first_view(request):
# Your view code
You will be redirected to your login URL at any point that you are not logged in.
Once you diagnose where you are being logged out, you should be able to troubleshoot much easier. If you are able to access every view, then you can troubleshoot your templates and your is_authenticated code.

django social-auth redirecting to error url

I integrate django social-auth in my app.In settings i have given
AUTHENTICATION_BACKENDS,FACEBOOK_APP_ID,FACEBOOK_API_SECRET, social_auth.context_processors,SOCIAL_AUTH_PIPELINE etc.
when i click on facebook login it is redirecting to facebook app login when logged in it is redirected back to my app but redirecting to LOGIN_ERROR_URL and the user is not authenticated.
LOGIN_REDIRECT_URL = '/'
LOGOUT_URL= '/logout/'
LOGIN_ERROR_URL = '/login-error/'
SOCIAL_AUTH_LOGIN_REDIRECT_URL="/home/"
How can i solve this and get my app authenticated? Same is happening with google login also.Please help.
Thanks.
This question is very similar to a recent one I just answered: django social auth error after completing pipeline . Over there, I said:
A good place to start would be to look in social-auth's views.py, at the few places where the redirect to LOGIN_ERROR_URL happens (the variable url is set to LOGIN_ERROR_URL and then HttpResponseRedirect(url) is called). Add some print statements, or better, set breakpoints using the python debugger. If you run your app in the Django development server, the print statements will show up in the terminal in which you ran the server. Otherwise, they may show up in your server logs, depending on your configuration. You may also find django-debug-toolbar helpful.
Using print statements or the debugger, my workflow would be:
Figure out what line in views.py the redirect is triggered from
Figure out what condition causes that line to be reached
Inspect the variables leading to that condition
Sorry this is so general. Happy to help more if you can provide some more specific information.
Aaron
encountered same mysterious redirect to error url.
For me problem was with a typo in an argument of custom pipeline method.
Fixing the typo fixed the problem.

Django Admin redirects not working

I am using the latest checkout of the django trunk - when I am in the admin on the "change" page for an object/item, there is a nice little link that says "view on site".
The link points to a url such as:
http://example.com:8888/admin/r/22/15/
However, when I click on that link (or enter that link into my browser) I get redirected to:
http://example.com:8888//example.com:8888/video/15
Which isn't a valid url - but it's really, really close ... the same is happening when I try to get_absolute_url for a comment. I get the short little redirect but it doesn't take me to the right page.
Any ideas why this is happening?
Additional Info (edit):
have tried with 1.1.1 (same problem)
I have one site listed under sites which is 'http://example.com:8888'
I thought maybe it had to do with the port number at the end - but what is strange is the my object's get_absolute_url works without a hitch.
I think the problem is coming from django.contrib.contenttypes.views.shortcut which is doing some funny appending business to handle cross-site things ... which I don't quite understand.
get_absolute_url (for objects)
#models.permalink
def get_absolute_url(self):
return ('video_detail', [str(self.id)])
I figured it out: my site's 'domain name' was listed as:
http://example.com
But the 'http' is what is throwing it off. When I removed it and just listed it as:
example.com
It works. I just have to update my email templates to include the 'http', I think ... unless django comes with a built in for adding that in its sites package. Off to investigate ...