Multiple app css,html, javascript,img

39 views
Skip to first unread message

Kazi Atik

unread,
Feb 17, 2017, 1:15:20 AM2/17/17
to Django users
Hi Everyone,

I am having multiple app in my project so i want to make globally these css, js,img folder because every time making css,js,img folder very difficult and slowing my development of
my project please give me solution 

myfb
users/
        __init__.py
        models.py
        views.py
        urls.py
        templates/
login.html
dashboard.html
logout.html
        css/
         style.css
-----------------------------------
categories/
        __init__.py
        models.py
        views.py
        urls.py
        templates/
post.html
friends.html
upload.html
        css/
         user.css
    img/
         fb.jpg
  JS/
  boot.min.js



Jani Tiainen

unread,
Feb 17, 2017, 2:58:29 AM2/17/17
to django...@googlegroups.com

Hi,

You need to put static files under static directory within app. Now you have static resources at your app root. So layout should be:

users/
    static/
        css/
            style.css

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/215f85be-b377-4f44-a3bb-0e9311f31c35%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Jani Tiainen

ludovic coues

unread,
Feb 17, 2017, 4:04:51 AM2/17/17
to django...@googlegroups.com
I second the need to put js and css files under a static directory. When going to production, you'll need to look at the management command about static files.

Also, don't put html files outside templates directory. And unless you have a good reason not to, put the html files in a subdirectory of the templates for named after the app. I now, it suck. But it let you have multiple app define an index.html template. Which is big plus.

Now, for your actual question. If the CSS isn't specific to an app, don't put it under that app. Create an extra app for all the generic stuff. Remember the part I was telling about subdirectory of templates ? Here you have a good reason to not have one. If all your html files start with a header and a link to your css, you are doing it wrong. Put the part that is common to all your page in a base.html file, with {% block content %}{% endblock %} where the content should be. Then post.html and friends.html will start with {% extends 'base.html' %}{% block content %}

I hope I am not being too confusing.


To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

-- 
Jani Tiainen

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Kazi Atik

unread,
Feb 17, 2017, 5:21:04 AM2/17/17
to Django users
what i have to do when i have multiple apps

ludovic coues

unread,
Feb 17, 2017, 6:23:03 AM2/17/17
to django...@googlegroups.com
Here is an exemple, slightly simplified from one of my project
Assuming a "generic" app, generic/templates/base.html would be
something like that

<!doctype html>
{% load staticfiles %}
<meta charset=utf-8>
<link rel=stylesheet href="{% static "css/style.css" %}">
<title>{% block title %}myfb{% endblock %}</title>
{% block content %}{% endblock %}

with style.css in generic/static/css/style.css and generic/js/dashboard.js

Then you can have users/templates/users/base.html like that

{% extends "base.html" %}
{% block title %}{{ block.super }}: user{% endblock %}
{% block content %}{% block main %}{% endblock %}{% endblock %}

then users/templates/users/dashboard.html

{% extends "users/base.html" %}
{% load staticfiles %}
{% block main %}
<script type=text/javascript src="{% static "js/dashboard.js" %}"></script>
the content of your dashboard here
{% endblock %}

categories/templates/categories/friends.html could be that:

{% extends "base.html" %}
{% block content %} list of friends {% endblock %}

generic would hold resources common to all other apps.
See the pattern ?
> https://groups.google.com/d/msgid/django-users/18065a52-9952-4c35-b185-7f330ca5af4d%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

ludovic coues

unread,
Feb 17, 2017, 7:32:40 AM2/17/17
to django...@googlegroups.com
You should use staticfiles to include your css, js and image in your page.
You first need to load the module in your template, then you can use
the static tag

{% load staticfiles %}
<link rel=stylesheet href="{% static "css/user.css" %}">

While you are in dev, static will look into each of your app's static
directory for the path you passed as argument.
When you will go in production, you will need to use manage.py
collectstatic and serve your static asset with your server

If the same static file is stored in multiple app, only the first one
will be served. You can use manage.py findstatic css/user.css to find
which file will be served when requesting css/user.css

2017-02-17 11:21 GMT+01:00 Kazi Atik <kazi....@gmail.com>:
> https://groups.google.com/d/msgid/django-users/18065a52-9952-4c35-b185-7f330ca5af4d%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Reply all
Reply to author
Forward
0 new messages