Django admin generates static url with /admin/ prefixed - python

I'm trying to setup a second development environment for my project. I've copied over the settings from the working setup but on the new one the URLs generated by the Django admin are prefixed with /admin/. This does not happen with the other setup at all.
More details:
I'm using django-grappelli for my admin panel.
The generated urls on the working setup are of the kind /static/grappelli while on the not-working one they are of the kind /admin/static/grappelli.
Disabling grappelli yields no fix either, the urls are then of the kind /admin/static/admin.
I'm using Django 1.5, it uses a git repository for the source files and a frozen pip requirements.txt. I can only conclude that version differences are highly unlikely.
Other static files work fine, only the admin related ones are hit with this.
My URL settings are the following:
STATIC_URL = '/static/'
MEDIA_URL = '/static/media/'
The *_ROOT ones are not set due to use of development server.
I'm out of ideas as to why this is happening on only one of the servers with the same setup.
edit:
Removing the /admin/ prefix allows you to properly access the files. The finder is fine, but the urls generated are just wrong.

The fix is very simple, the STATIC_URL setting should always contain the first /. It seems the Django admin prepends /admin/ if there is none.
I had a faulty settings file overwriting my default STATIC_URL, always check your settings first!

Related

Why my django static file have number in the end

I have to debug some existing Django project build me other develeopers.
In my local developement environment all the static files have path like
/static/myapp/module/user.js
But when i see the html of testing on tetsing server then i can see the files like
static/myapp/module/user.42323gdb.js
Now i want to know why the system is using that file instead of user.js.
Is there any setting which i can fix or it is meant to do like that
Seems like your project uses one of the asset managers. I suspect you can find it in the INSTALLED_APPS setting.
UPDATE: By default the django-pipeline does this magic then the DEBUG = False. Which is the case for your development environment.
The other setting to enable/disable the pipeline is the PIPELINE_ENABLED. So you can have the "normal" file names on the production server too. But I suggest you to leave the pipeline enabled :-)

Django tutorial - can't connect to the page after modifying a template

From the Django tutorial part 2:
Create a templates directory in your project directory. Open your
settings file (mysite/settings.py, remember) and add a
TEMPLATE_DIRS setting:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
So I created a folder called 'templates' in mysite folder, and copied the TEMPLATE_DIR settings above to settings.py in a pretty much random place. Next step:
Now create a directory called admin inside templates, and copy the
template admin/base_site.html from within the default Django admin
template directory in the source code of Django itself
(django/contrib/admin/templates) into that directory.
I followed the instructions - I created another folder called admin inside my templates folder, and copied a file called base_site.html from django/contrib/admin/templates into my new admin folder.
But when I tried to reopen my website again it said: "Chrome couldn't connect to 127.0.0.1:8000". So I reversed all the changes (deleted the TEMPLATE_DIR configuration from settings.py and the templates folder), but my page still won't open.
Any ideas what could've happened? (I'd hate to go through the tutorial from the beginning)
If your browser says it cannot connect to the server, then the web server is not responding. Are you sure you are still running the web server? ;)
If there had been errors in your setup, you would have recieved a 500 server error, or the Django debug template with the stacktrace that occured.
Well, that's embarrassing. I don't really know what a server is so I didn't pay much attention to python manage.py runserver. Now I know that you have to run the server with this command before you open 127.0.0.1:8000 in a browser. (This was covered in the tutorial but way back..)
Thanks yuvi!

How to migrate Django project to Pythonanywhere

I am trying to set up a Django app on Pythonanywhere — I've managed to figure out Bitbucket and clone the code in — I deleted the files in the directory that was provided for me — but can't get it to work.
I've done 'syncdb', then when I go to what I think is the correct URL for the app, I keep getting "Unhandled exception" — The error is that it can't find 'portfolio.settings' in an import (portfolio is the name of the app)
I also have no idea what to put for MEDIA_ROOT and STATIC_DIRS — these should be, as far as I know, full paths, not relative.
I'm a Django newbie, and this is proving rather overwhelming, to get the app, which functions fine locally, deployed. Any help much provided (I haven't found the Pythonanywhere forums — which don't seem indexed -- or help all that helpful, I'm afraid)
I also thought: why don't I let Pythonanywhere set up a blank project for me, but again, I don't know how to handle STATIC_DIRS and MEDIA_ROOT, and I don't really know how to make my project fit their setup.
Thanks for any help.
For anyone else that runs into similar issues: import errors in web applications are typically to do with your sys.path not being correctly configured. Check your WSGI file (on PythonAnywhere, you can find it on your web tab. Other hosts may do things differently).
Example:
/home/myusername/myproject/ is the project folder
/home/myusername/myproject/my_cool_app/ is one of your app folders
/home/myusername/myproject/myproject/settings.py is the settings file location
Your WSGI file should have:
sys.path.append('/home/myusername/myproject')
# ...
DJANGO_SETTINGS_MODULE = 'myproject.settings'
And your settings.py should have
INSTALLED_APPS = (
#...
'my_cool_app'
Everything has to line up so that the dot-notation names of your app in INSTALLED_APPS and your DJANGO_SETTINGS_MODULE environment variable will import correctly, relative to the folder you add to sys.path.
So in the example above, you could conceivably do:
# wsgi file
sys.path.append('/home/myusername')
DJANGO_SETTINGS_MODULE = 'myproject.myproject.settings'
# settings.py
INSTALLED_APPS = 'myproject.my_cool_app'
But don't do that, it's overcomplicated.
PS there's a detailed guide to sys.path and import issues for pythonanywhere in the docs.

Django-profile and django-registration get started

I've installed Django-registration succesfully (form this tutorial).
So now I have a Django project "loginSystem", with in this map urls.py, settings.py, manage.py and _init_.py, so there is no app created but it works. The registration-folder is installed in my python-packages folder.
No I want to give to each users a unique profile, so I'll do this manual for django-profiles. The profiles folder is also installed in my python-packages folder.
But what do I have to create now to follow and start this django-profiles manual?
Because they start speaking about a "/profiles/edit"-folder...
So do I have to work in the dist-packages/profiles folder, or in my project-folder (without app)?
Thanks a lot, it asked me already a lot of time without result...
I don't see there they are talking about a /profiles/edit/ folder. The only thing you have to do is creating a /profiles/ folder in your TEMPLATE_DIR and put an edit_profile.html template there. Then you still need to hock up the url configuration to serve the edit view of the profiles app (they suggest a url somewhere like /profiles/edit/) and then you are good to go. Its basically the same as for django-registration except django-registration already create the url configuration for you.

Django 1.3 - Admin pages do not get the correct ADMIN_MEDIA_PREFIX settings

In my projects settings.py I have set
ADMIN_MEDIA_PREFIX='/static/admin/'
The admin media is being served in the correct location, browsing to http://127.0.0.1:8000/static/admin/css/base.css gives me the base.css for the admin page. But when I inspect the admin pages they are still attempting to find the admin media at '/media/admin/'. I'm not sure what is wrong here. This is what I get when I attempt to find the setting in a manage.py shell.
>>> from django.conf import settings
>>> settings.ADMIN_MEDIA_PREFIX
'/media/admin/'
This should be '/static/admin/'.
James was right. I had a second settings.py defined for my development environment that was overriding my main settings. I've deleted ADMIN_MEDIA_PREFIX from that file and now everything works as expected. Thank you!!
I sometimes get a similar problem where I've clearly made changes but the code still behaves a la pre-change. Sometimes making sure I've saved with fix it, but other times the compiled python files (.pyc) don't seem to update. Deleting the .pyc file sometimes fixes my issue. Note that when you ./manage.py runserver again, the .pyc will be generated with the updated code.
Maybe try deleting your settings.pyc?

Categories

Resources