{"title":"Γράφοντας το πρώτο σας Django app, μέρος 6","version":"3.1","locale":"el","docname":"intro/tutorial06","url":"/el/3.1/intro/tutorial06/","canonical":"https://djangodocs.dev/el/3.1/intro/tutorial06/","summary":"Ο οδηγός αυτός ξεκινά εκεί που τελειώνει ο οδηγός 5 . Έχουμε φτιάξει μια γεμάτη με τεστ εφαρμογή ψηφοφορίας (Web-poll) και τώρα θα βελτιώσουμε την εμφάνιση της…","html":"<h1>Γράφοντας το πρώτο σας Django app, μέρος 6<a class=\"heading-anchor\" href=\"#writing-your-first-django-app-part-6\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Ο οδηγός αυτός ξεκινά εκεί που τελειώνει <a class=\"reference internal\" href=\"/el/3.1/intro/tutorial05/\"><span class=\"doc\">ο οδηγός 5</span></a>. Έχουμε φτιάξει μια γεμάτη με τεστ εφαρμογή ψηφοφορίας (Web-poll) και τώρα θα βελτιώσουμε την εμφάνιση της προσθέτοντας ένα stylesheet (CSS) και μια εικόνα.</p>\n<p>Πέρα από την HTML που παράγεται από τον server, τα web applications, σε γενικές γραμμές, χρειάζονται να κάνουν serve και πρόσθετα αρχεία — όπως εικόνες, γραμματοσειρές, JavaScript αρχεία, CSS κλπ — αρχεία απαραίτητα ούτως ώστε μια πλήρης ιστοσελίδα να γίνει render. Στο Django, αναφερόμαστε σε αυτά τα αρχεία ως «static files».</p>\n<p>For small projects, this isn’t a big deal, because you can keep the static\nfiles somewhere your web server can find it. However, in bigger projects –\nespecially those comprised of multiple apps – dealing with the multiple sets\nof static files provided by each application starts to get tricky.</p>\n<p>Αυτό ακριβώς κάνει το <code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code>: συγκεντρώνει όλα τα static files από κάθε εφαρμογή σας (και οποιοδήποτε άλλο μέρος εσείς καθορίσετε) σε μια τοποθεσία (που εσείς πάλι καθορίζετε) από την οποία μπορούν εύκολα να γίνουν served σε παραγωγικό περιβάλλον (production).</p>\n<aside class=\"admonition-where-to-get-help admonition\">\n<p class=\"admonition-title\">Που να ψάξετε για βοήθεια</p>\n<p>If you’re having trouble going through this tutorial, please head over to\nthe <a class=\"reference internal\" href=\"/el/3.1/faq/help/\"><span class=\"doc\">Getting Help</span></a> section of the FAQ.</p>\n</aside>\n<section id=\"customize-your-app-s-look-and-feel\">\n<h2>Προσαρμογή της εμφάνισης της <em>εφαρμογής</em> σας<a class=\"heading-anchor\" href=\"#customize-your-app-s-look-and-feel\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Πρώτα, δημιουργήστε ένα φάκελο με το όνομα <code class=\"docutils literal notranslate\"><span class=\"pre\">static</span></code> μέσα στο φάκελο <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> (μέσα, δηλαδή, στην εφαρμογή σας). Το Django θα ψάξει για τυχόν static files μέσα εκεί, ομοίως όπως ψάχνει για templates μέσα στο φάκελο <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/templates/</span></code>.</p>\n<p>Η ρύθμιση του Django <a class=\"reference internal\" href=\"/el/3.1/ref/settings/#std-setting-STATICFILES_FINDERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATICFILES_FINDERS</span></code></a> περιέχει μια λίστα από finders οι οποίοι γνωρίζουν πως να βρίσκουν τα static files από διάφορα μέρη. Μια από τις προεπιλογές είναι η <code class=\"docutils literal notranslate\"><span class=\"pre\">AppDirectoriesFinder</span></code> η οποία κοιτάζει να βρει έναν υπό-φάκελο με το όνομα «static» σε κάθε εφαρμογή η οποία συγκαταλέγεται στη λίστα της ρύθμισης <a class=\"reference internal\" href=\"/el/3.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> (όπως ο υπό-φάκελος που μόλις δημιουργήσαμε μέσα στην εφαρμογή <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code>). Το admin site χρησιμοποιεί την ίδια αρχιτεκτονική φακέλων για τα δικά του static files.</p>\n<p>Within the <code class=\"docutils literal notranslate\"><span class=\"pre\">static</span></code> directory you have just created, create another directory\ncalled <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> and within that create a file called <code class=\"docutils literal notranslate\"><span class=\"pre\">style.css</span></code>. In other\nwords, your stylesheet should be at <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/static/polls/style.css</span></code>. Because\nof how the <code class=\"docutils literal notranslate\"><span class=\"pre\">AppDirectoriesFinder</span></code> staticfile finder works, you can refer to\nthis static file in Django as <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/style.css</span></code>, similar to how you reference\nthe path for templates.</p>\n<aside class=\"admonition-static-file-namespacing admonition\">\n<p class=\"admonition-title\">Static file namespacing</p>\n<p>Just like templates, we <em>might</em> be able to get away with putting our static\nfiles directly in <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/static</span></code> (rather than creating another <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code>\nsubdirectory), but it would actually be a bad idea. Django will choose the\nfirst static file it finds whose name matches, and if you had a static file\nwith the same name in a <em>different</em> application, Django would be unable to\ndistinguish between them. We need to be able to point Django at the right\none, and the best way to ensure this is by <em>namespacing</em> them. That is, by\nputting those static files inside <em>another</em> directory named for the\napplication itself.</p>\n</aside>\n<p>Τοποθετήστε τον ακόλουθο κώδικα στο αρχείο CSS (<code class=\"docutils literal notranslate\"><span class=\"pre\">polls/static/polls/style.css</span></code>):</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"css\"><figcaption class=\"code-block-caption\">polls/static/polls/style.css</figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Css</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Css code\"><code><span class=\"nt\">li</span><span class=\"w\"> </span><span class=\"nt\">a</span><span class=\"w\"> </span><span class=\"p\">{</span>\n<span class=\"w\">    </span><span class=\"k\">color</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"kc\">green</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n</code></pre></figure>\n<p>Μετά, προσθέστε τα ακόλουθα στην αρχή του αρχείου <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/templates/polls/index.html</span></code>:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"html+django\"><figcaption class=\"code-block-caption\">polls/templates/polls/index.html</figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">static</span> <span class=\"cp\">%}</span>\n\n<span class=\"p\">&lt;</span><span class=\"nt\">link</span> <span class=\"na\">rel</span><span class=\"o\">=</span><span class=\"s\">&quot;stylesheet&quot;</span> <span class=\"na\">type</span><span class=\"o\">=</span><span class=\"s\">&quot;text/css&quot;</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">static</span> <span class=\"s1\">&#39;polls/style.css&#39;</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span><span class=\"p\">&gt;</span>\n</code></pre></figure>\n<p>Το template tag <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">static</span> <span class=\"pre\">%}</span></code> παράγει το absolute URL των static files.</p>\n<p>That’s all you need to do for development.</p>\n<p>Start the server (or restart it if it’s already running):</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-0-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>runserver\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-0-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py manage.py runserver\n</code></pre></div></div></div>\n<p>Reload <code class=\"docutils literal notranslate\"><span class=\"pre\">http://localhost:8000/polls/</span></code> and you should see that the question\nlinks are green (Django style!) which means that your stylesheet was properly\nloaded.</p>\n</section>\n<section id=\"adding-a-background-image\">\n<h2>Προσθέτοντας μια background-image<a class=\"heading-anchor\" href=\"#adding-a-background-image\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Επόμενο βήμα είναι να δημιουργήσουμε έναν υπό-φάκελο για τις εικόνες μας. Δημιουργήστε, λοιπόν, ένα φάκελο με το όνομα <code class=\"docutils literal notranslate\"><span class=\"pre\">images</span></code> στη διαδρομή <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/static/polls/</span></code>. Μέσα σε αυτό το φάκελο αποθηκεύστε μια εικόνα με το όνομα <code class=\"docutils literal notranslate\"><span class=\"pre\">background.gif</span></code>. Με άλλα λόγια το full path της εικόνας θα είναι το <code class=\"docutils literal notranslate\"><span class=\"pre\">polls/static/polls/images/background.gif</span></code>.</p>\n<p>Μετά προσθέστε στο CSS αρχείο σας τα παρακάτω (<code class=\"docutils literal notranslate\"><span class=\"pre\">polls/static/polls/style.css</span></code>):</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"css\"><figcaption class=\"code-block-caption\">polls/static/polls/style.css</figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Css</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Css code\"><code><span class=\"nt\">body</span><span class=\"w\"> </span><span class=\"p\">{</span>\n<span class=\"w\">    </span><span class=\"k\">background</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"kc\">white</span><span class=\"w\"> </span><span class=\"nb\">url</span><span class=\"p\">(</span><span class=\"s2\">&quot;images/background.gif&quot;</span><span class=\"p\">)</span><span class=\"w\"> </span><span class=\"kc\">no-repeat</span><span class=\"p\">;</span>\n<span class=\"p\">}</span>\n</code></pre></figure>\n<p>Reload <code class=\"docutils literal notranslate\"><span class=\"pre\">http://localhost:8000/polls/</span></code> and you should see the background\nloaded in the top left of the screen.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Προειδοποίηση</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">static</span> <span class=\"pre\">%}</span></code> template tag is not available for use in static files\nwhich aren’t generated by Django, like your stylesheet. You should always\nuse <strong>relative paths</strong> to link your static files between each other,\nbecause then you can change <a class=\"reference internal\" href=\"/el/3.1/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_URL</span></code></a> (used by the\n<a class=\"reference internal\" href=\"/el/3.1/ref/templates/builtins/#std-templatetag-static\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">static</span></code></a> template tag to generate its URLs) without having to modify\na bunch of paths in your static files as well.</p>\n</aside>\n<p>Αυτά είναι τα <strong>βασικά</strong>. Για περισσότερες πληροφορίες σχετικά με τις ρυθμίσεις και άλλα εργαλεία τα οποία συμπεριλαμβάνονται στο Django δείτε στο άρθρο <a class=\"reference internal\" href=\"/el/3.1/howto/static-files/\"><span class=\"doc\">διαχείριση των static files</span></a> και στο <a class=\"reference internal\" href=\"/el/3.1/ref/contrib/staticfiles/\"><span class=\"doc\">η εφαρμογή staticfiles</span></a>. Το άρθρο <a class=\"reference internal\" href=\"/el/3.1/howto/static-files/deployment/\"><span class=\"doc\">production server και static files</span></a> συζητά πως να χρησιμοποιήσετε τα static files σε ένα πραγματικό server.</p>\n<p>Όταν είστε εξοικειωμένοι με τα static files, διαβάστε το <a class=\"reference internal\" href=\"/el/3.1/intro/tutorial07/\"><span class=\"doc\">έβδομο μέρος αυτού του οδηγού</span></a> για να μάθετε περισσότερα με την παραμετροποίηση του διαχειριστικού site του Django (admin site) το οποίο δημιουργείται αυτόματα.</p>\n</section>","rootId":"writing-your-first-django-app-part-6","toc":[{"title":"Προσαρμογή της εμφάνισης της εφαρμογής σας","anchor":"customize-your-app-s-look-and-feel","children":[]},{"title":"Προσθέτοντας μια background-image","anchor":"adding-a-background-image","children":[]}],"breadcrumbs":[{"docname":"intro/index","title":"Ξεκινώντας","url":"/el/3.1/intro/"}],"prev":{"docname":"intro/tutorial05","title":"Γράφοντας το πρώτο σας Django app, μέρος 5","url":"/el/3.1/intro/tutorial05/"},"next":{"docname":"intro/tutorial07","title":"Γράφοντας το πρώτο σας Django app, μέρος 7","url":"/el/3.1/intro/tutorial07/"},"formats":{"html":"/el/3.1/intro/tutorial06/","markdown":"/el/3.1/intro/tutorial06.md","json":"/el/3.1/intro/tutorial06.json"},"source":"https://github.com/django/django/blob/stable/3.1.x/docs/intro/tutorial06.txt","official":"https://docs.djangoproject.com/el/3.1/intro/tutorial06/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}