Google App Engine Project hierarchy - django

I'm working on a google app engine (with Django) and I just can't figure out what's a good practice for folder hierarchy.. I've looked at this: Project structure for Google App Engine
but one thing isn't clear - what if I have static folder (like js files) that are unique to my app, not project? where do they go?
my current hierarchy is:
proj
static
** js
** css
myapp
** templates
So when a template inside my app sends a GET for js/script.js. this gets redirected to /myapp/js/script.js, which my server doesn't recognize.
here is my project url.py:
urlpatterns = patterns('',
(r'^myapp/', include('myapp.urls')),
)
and here is my myapp.urls.py:
urlpatterns = patterns('myapp.views',
(r'^$', 'myapp.views.index'),
)
how should I rearrange this to work?
thanks!

Why not just use absolute URLs? Prefix your references to static resources with a /, and all will be well.

Related

what is the use of creating a new way of url pattern in this code and what does it symbolize?

Blockquote
file path of the url
hello guys i am just a beginner in django so i cant understad what is the use of using this url pattern in this code
from django.conf import settings from django.conf.urls.static import static
urlpatterns += static(settings.MEIDIA_URL, document_root=settings.MEDIA_ROOT)
since user-uploaded content is assumed to exist in a production context, to see media
items locally we need to update config/urls.py to show the files locally. This involves importing
both settings and static at the top and then adding an additional line at the bottom.

Changing URL pattern in Django so it doesn't show app name

I've got a folder/app named "projects" and there I want to make a new site there, named, let's say, "cheese".
In my urlpatterns in urls.py in projects folder I've got
path('cheese/', views.cheese, name='cheese')
but the whole URL looks like domain.co/projects/cheese
I'd like to omit that projects in URL, so it could just be domain.co/cheese
Those URL-things are kinda hard to understand for me and as I couldn't properly name my problem, I couldn't find a solution out there, though I believe there must be some.
The idea of having an app inside another app is kinda weird, but anyways, that won't enforce the inner app's urls to be like <outer_app>/<inner_app>/... if you set the url patterns correctly.
Basically, you've got domain.co/projects/cheese because, you include your project's app urls as:
# main urls.py file for your project
path('projects/', include('projects.urls'))
and in your project's urls file you have:
# projects/urls.py
path('cheese/', include('projects.cheese.urls'))
So if you want to have the cheese urls as domain.co/cheese, simply add the include to the main url file:
# main urls.py file for your project
path('projects/', include('projects.urls'))
path('cheese/', include('projects.cheese.urls'))

Django root site in virtual directory

The following is the setup:
I have a virtual directory in IIS 6 in which my Django app lives, IIS is configured to pass every request on that virtual directory to the Django WSGI handler
Let's say this is domain.com/virtual/
In my Django dev URL CONF I had urls configured like this: url( r'^home$, 'project.views.home' )
Question:
Is there an easy way (through Django settings OR server settings) to set some kind of ROOT_URL for the django app? (Without manually prepending as this is incompatbile with i18n_patterns)
Django should treat all patterns as rooted in the ROOT_URL and redirect - again - relative to that same ROOT URL.
Thank you in advance
I have done this in the past.
In your settings file add a setting called something like:
VIRTUAL_DIRECTORY = "your_virtual_directory/"
If you are doing dev or have this behind it's own site domain you can keep it blank.
Then in your site's urls.py file add this to all of your top level url patterns:
from django.conf import settings
urlpatterns = patterns('',
url(r'%sadmin' % settings.VIRTUAL_DIRECTORY),
)
Do the string pattern for all of your url patterns. You will only need to do this at the site level urls.py not for every app. This way if you have a non blank VIRTUAL_DIRECTORY then it will prefix it to all of your top level url patterns.

Getting Page not found (404) in Django 1.3.1 from bad url pattern

I am following along with this Django blog tutorial and can not get the url pattern given in the tutorial to work properly. The url.py code the author gives is
(r'^static/(?P
.*)$', 'django.views.static.serve',
{'document_root': 'c:/static/adornment'}),)
and I adapted it to my Linux set up like this
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
(r'^static/(.*)$', 'django.views.static.serve',
{'document_root': '/home/sez/blog/static/image.png'}
),
)
and after going to http://127.0.0.1:8000/static/image.png I received the following error
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/image.png
'image.png' could not be found
How can I make this work?
Change up your 'document_root' to be a directory, and make sure your STATIC_URL setting is set to /static/
The likely reason for the behavior you were experiencing is as follows. You probably have 'django.contrib.staticfiles' included in your INSTALLED_APS in the "settings.py" file. In that case, when you run "manage.py runserver" (and you have DEBUG = True in your "settings.py"), static files will be automatically served from your STATIC_URL by the staticfiles app and not by your 'django.views.static.serve' URL pattern. However, in your case staticfiles app is not set up correctly and won't find your 'image.png' file. You might want to read more about conventional serving of static files here: Managing static files.
For that matter, STATIC_URL is not supposed to be a filesystem path like you have now: STATIC_URL.
When you made it that way, you effectively disabled staticfiles app and your code started working as a result :)
So, to solve your problem "correctly", you need to either make sure that STATIC_URL and the URL path in your "urls.py" are different, for example, make one of them "/static/" and another one "/media/" (or something else), or just remove "django.contrib.staticfiles" from your INSTALLED_APPS altogether if you don't use it. (And, of course, the advice about making 'document_root' a directory was entirely correct. You can read more about using 'django.views.static.serve' here: Serving static files in development.)

Django : How do I call "reverse" on a link to a static image file?

In Django, I have images stored in site_media like this :
/site_media/somepath/image.jpg
The urls.py file has been set up to server these using :
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'staticfiles.views.serve')
)
urlpatterns += patterns('',
(r'^site_media/(?P<path>.*)$', 'staticfiles.views.serve')
)
So how can I make a reverse() or {% url %} call to one of these images?
It's probably easier to just use:
{{ MEDIA_URL }}somepath/image.jpg
assuming you've set up the MEDIA_URL setting in your settings.py.
Also, you really don't want to serve static files through Django in a production environment. From the docs:
Using this method is inefficient and insecure. Do not use this
in a production setting. Use this only for development.
One more thing - what is staticfiles.views.serve? Django has a built in system for serving static files, though the disclaimer above still applies.
You probably want to serve static files straight from Apache/nginx/whatever. It'll be heaps quicker.