Changing settings in settings.py via the webpage/template - django

I have been recently asked to "hack" into django/python and edit a project. I have never programmed in anything as close to these two languages, so I am studying them on the go.
I did my homework and searched through couple of topics
Can I access constants in settings.py from templates in Django?
Django - app to edit the constants in settings.py in admin interface
and then
http://blog.chipx86.com/2008/08/05/django-development-with-djblets-dynamic-site-configuration/
However, since this is not something I programmed, I have been asked to add some features to admin configuration page to be editable from there and enhance some other portions of the website. I also understand that those require restart of django server.
The guy that did this had site_config.py and settings.py . From the live version of the project, only accessible/editable constants were those from site_config.py. I managed to reproduce those this is how a portion of site_config.py looks like.
## SITE #
# Populate default site config values here
SITE_DISPLAY_NAME = 'testing123'
SITE_COMPANY_NAME = 'another123'
Now the question is how would I add a portion from urls.py to the given site_config.py
urlpatterns = patterns('',
# We don't yet have a top-level main view, redirect to AIM app
(r'^$', 'django.views.generic.simple.redirect_to', {'url': '/aim/'}),
This is the portion that I would like to incorporate to the site_config.py. What would I have to do?
If you need more information, please let me know, and I am sorry if this is a newbie question, but I really never worked on anything like this before.
Thank you.

Related

Django Upgrade from 1.11 to 2.2.1 Problem with URLs and Paths

Today I decided to upgrade my project from Django 1.11 to 2.2.1. I've been working through various issues with my project, and I'm fighting through them. However, I spent most of tonight trying to get the URLs to work and they won't cooperate. Long story short, I have multiple app in my project and each app has a URL with it's own namespace. In Django 1.11 this is working fine. However, when I try to port the logic over to Django 2.2.1, I keep getting an error saying that I have a circular import somewhere probably.
Here is a snippit of what works just fine in Django 1.11.......
My Main Project...In Django 1.11
url(r'^Main/',include('AppA.urls',namespace="AppA")),
But when I try to do this in Django 2.2.1.....
I realize that URLs were replaced by paths...
path('', include('AppA.urls')),
But when I try to start my application it says....
your project does not appear to have any patterns in it. If you see valid p
atterns in the file then the issue is probably caused by a circular import.
I can't seem to figure out how to create the namespace that is working in django 1.11 so that I can properly reference my urls in my templates.
I've been staring at this most of the evening and that might be why I'm not seeing it...I also looked at the Django doc...https://docs.djangoproject.com/en/2.2/topics/http/urls/
And I just can't see what I might be doing wrong. Thanks in advance for any help to get me back on track.
As Bloodmallet pointed out to me...
I needed to add....
app_name = 'Appa'
to the top of my urls.py file. After doing this, the path URL worked as expected.
Instead of path(), consider using re_path():
from django.urls import include, re_path
re_path(r'^Main/',include('AppA.urls',namespace="AppA")),

Django reverts to default urls

I'm working with Django 1.6.4 and I've constructed a login system that appears to be working up until the back button is pressed in the browser. The URL giving me fits is http://127.0.0.1:8000/login. Here is my project URLconf:
urlpatterns = patterns('',
(r'', include('apps.mylogin.urls')),
)
(Before any of you Django veterans point out the empty regex pattern :) – I've tried a number of different ones that have been suggested on the interwebs: ('^', '^$'), and even tried moving the app's URLconf into the project's URLconf and all of them seem to yield this error. Of course if you detect shenanigans in this code, feel free to point it out, I like best practices :D)
The URLconf contained in the include is as follows:
urlpatterns = patterns('apps.mylogin.views',
url(r'^login/$', 'login', name='login'),
url(r'^logged_in/$', 'logged_in', name='logged_in'),
)
Let's say I land on the login page and I don't enter any information, I just click submit; the LoginForm class that I've created will (during the clean method) return a raise ValidationError. Everything works as expected: all the appropriate errors are displayed to the user and life is peachy... until I hit the back button. It is at this point I'm presented with a captured 404 page that says it was a GET request sent to http://127.0.0.1:8000/login with the following read out:
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^admin/
^account/
The current URL, login/, didn't match any of these.
I'm assuming these URLs are some part of a 'base' set defined by Django; which is well enough and all, but I'm quite curious as to why it appears to have forgot my URLs and suddenly can't find the location I was just at.
Any help would be greatly appreciated and if there is any additional information required please let me know and I'll attempt to edit this question to reflect it.
After yet additional testing and thought on the matter it appears that there is a conflict of caching mechanisms. It appears the conflict is produced when using certain Safari extensions, causing what appeared to be an injection of information; when in reality it was using code from several tutorials that had run before it.
I've arrived at this conclusion by testing Chrome, Firefox and Safari all without any extensions or enhancements beyond their default configuration. It is only when I return to Safari and enable a specific extension (and what is more absurd is that this particular extension does not inhibit my desktop ... it might be likely that there are additional extensions working in conjunction to cause this) that I receive the captured 404 error along with the seemingly injected ^account/.

django specific settings app

I am working on a django app that needs a directory to download and store files.
I want to keep my app reusable so I do not want to hard code the path of this directory.
So I want to make this path a setting/a global variable that can be set up.
Where could I put this setting/global variable?
Is this kind of approach good ?
http://blog.muhuk.com/2010/01/26/developing-reusable-django-apps-app-settings.html
Thanks for your advice!
I use the following methodology:
# some file in your app:
from django.conf import settings
MY_APP_SETTING = getattr(settings, 'MY_APP_SETTING', 'some default value')
This effectively allows end-users to customized the setting in their own settings.py, but still ensures that there's always some default value set. You can now use MY_APP_SETTING at will in the rest of your code.
UPDATE
The link in your question was taking too long to load, so I just went ahead and answered. As it turns out, the method I suggested is the same as what it suggests, so yes, I'd consider that approach good ;).

How to add app to all pages in django?

I have an app called lastapp that I would like it to be viewable on all pages. It renders base.html so it's available on all apps. So far I've tried adding it to the urls.py like this:
urlpatterns = patterns('',
(r'^first/$', firstapp, lastapp),
(r'^last/$', lastapp),
)
But when I go to /first/ it gives me the error: Error, 'function' not iterable. It shows fine when going to /last/. If I remove lastapp from the /first/ site in urls.py, then firstapp shows fine also. Is there a special way to do this? Thanks in advance.
What exactly are you trying to do?
All installed applications are available in your project, but the application by it self does not return any data to the template, that's the job of the views which are part of that application.
My guess is that firstapp and lastapp are rather views then apps.
If that is a case, context processors make it possible to always pass certain data to a template.
So, create a context processor out of lastapp and set it in TEMPLATE_CONTEXT_PROCESSORS in settings.py.
I think what you need is probably Middleware.

Django's Satchmo and flatpages issue

I'm having a problem with configuring Flatpages in Satchmo. I've used them before, in a pure django app, but now it just doesn't work, returning 301 http error when I'm trying to enter a flatpage-configured site.
What I've done to configure it:
added the middleware "django.contrib.flatpages.middleware.FlatpageFallbackMiddleware" to MIDDLEWARE_CLASSES as last in the list,
configured example pages in admin module.
Simply what the docs say about flatpages config.
I'm feeling helpless. Don't know how could I debug this issue. Any thoughts on that?
And of course help appreciated.
Thanks to suggestion by Peter I've managed to narrow the problem to my urls.py file, for the satchmo shop.
The urlpatterns has only one entry:
(r'', 'django.views.generic.simple.redirect_to', {'url' : '/shop/'}),
This version does not work and moreover interfere with flatpages. But disabling flatpages from MIDDLEWARE_CLASSES and adding it to urls.py like the snippet below works:
(r'^(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage'),
(r'', 'django.views.generic.simple.redirect_to', {'url' : '/shop/'}),
However next problem is with the redirection from / to /shop/. With the above config it results in infinite loop.
Perhaps you know the reason for that behavior (redirect overriding flatpage) and maybe You could suggest some working solution to this problem or what should be done with requests to /.
It returns 301? That's Page Moved Permanently (HttpResponsePermanentRedirect) and there are no references to that in the flatpages directory so I don't think it's coming from there. In fact there are only about 5 references to HttpResponsePermanentRedirect in all of the standard 1.1.1 release.
Possible approaches:
Comment out the FlatPages middleware and see if the error changes (I'm betting it won't).
Try changing the order of your MIDDLEWARE classes and see if things change.
When presenting a problem like this it's better to get very specific by showing the exact code from applicable portions of settings.py (or whatever) and by giving other things like precise URLs and the urls.py patterns you are trying to match.
Update:
OK, some random thoughts:
The pattern (r'^(?P<url>.*)$', 'django.contrib.flatpages.views.flatpage'), will match anything. Any patterns after it will never be seen.
flatpages doesn't work by being called directly, it does its magic in middleware. It looks for 404 responses (Page Not Found) and then looks to see if that path exists in its table. If so, it calls a view that renders the page, etc. etc. If it doesn't find a match it let's the 404 continue on through middleware processing.
The pattern (r'', 'django.views.generic.simple.redirect_to', {'url' : '/shop/'}), will match anything (I just tested it). If you want to match an empty path, use r('^$', etc.). This is the source of your infinite loop.
If you are new to regular expressions the Django urls.py file can seem like F*cking Magic. I recommend starting very simply and add one rule at a time. Do some quick tests to ensure that the new rule a) matches stuff you want it to match, and b) doesn't match stuff it shouldn't. In particular, make sure that some of the rules that occur later in the file are still reachable. In this case they wouldn't have been which should have raised a red flag.