{"title":"Γράφοντας το πρώτο σας Django app, μέρος 1","version":"4.0","locale":"el","docname":"intro/tutorial01","url":"/el/4.0/intro/tutorial01/","canonical":"https://djangodocs.dev/el/4.0/intro/tutorial01/","summary":"Ο μόνος τρόπος να μάθετε να γράφετε Django apps (εφαρμογές) και γενικά να δουλεύετε με το Django είναι μέσα από παραδείγματα.","html":"<h1>Γράφοντας το πρώτο σας Django app, μέρος 1<a class=\"heading-anchor\" href=\"#writing-your-first-django-app-part-1\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Ο μόνος τρόπος να μάθετε να γράφετε Django apps (εφαρμογές) και γενικά να δουλεύετε με το Django είναι μέσα από παραδείγματα.</p>\n<p>Μέσα από αυτόν τον οδηγό, θα φτιάξουμε παρέα μία απλή εφαρμογή (application) ψηφοφορίας (poll app). Κάθε ερώτηση θα απαρτίζεται από ένα σετ απαντήσεων, από τις οποίες ο χρήστης θα μπορεί να επιλέξει μία κάθε φορά.</p>\n<p>Η εφαρμογή θα αποτελείται από δύο μέρη:</p>\n<ul class=\"simple\">\n<li><p>Ένα κοινό-δημόσιο site που θα επιτρέπει στους χρήστες να βλέπουν τις διαθέσιμες ψηφοφορίες (γκάλοπ, αν προτιμάτε) και να ψηφίζουν.</p></li>\n<li><p>Ένα διαχειριστικό (admin) site το οποίο θα επιτρέπει σε εσάς (που θα είστε ο διαχειριστής) να προσθέτετε νέες, να αλλάζετε ήδη υπάρχουσες αλλά και να διαγράφετε ψηφοφορίες.</p></li>\n</ul>\n<p>Υποθέτουμε ότι έχετε ήδη <a class=\"reference internal\" href=\"/el/4.0/intro/install/\"><span class=\"doc\">εγκαταστήσει το Django</span></a> στον υπολογιστή σας. Μπορείτε να δείτε αν το Django έχει εγκατασταθεί (και αν ναι, ποια έκδοση διαθέτετε) πληκτρολογώντας στην κονσόλα (γραμμή εντολών):</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 -m django --version\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 -m django --version\n</code></pre></div></div></div>\n<p>Αν το Django είναι εγκατεστημένο, θα δείτε την έκδοση του. Αν δεν είναι, θα δείτε ένα σφάλμα λέγοντας σας ότι “δεν υπάρχει κάποιο module με το όνομα django” (no module named django).</p>\n<p>This tutorial is written for Django 4.0, which supports Python 3.8 and\nlater. If the Django version doesn’t match, you can refer to the tutorial for\nyour version of Django by using the version switcher at the bottom right corner\nof this page, or update Django to the newest version. If you’re using an older\nversion of Python, check <a class=\"reference internal\" href=\"/el/4.0/faq/install/#faq-python-version-support\"><span class=\"std std-ref\">Ποια έκδοση της Python να χρησιμοποιήσω με το Django;</span></a> to find a compatible\nversion of Django.</p>\n<p>Δείτε See <a class=\"reference internal\" href=\"/el/4.0/topics/install/\"><span class=\"doc\">πως να εγκαταστήσετε το Django</span></a> για συμβουλές απεγκατάστασης τυχόν παλιών εκδόσεων και εγκατάστασης της καινούργιας.</p>\n<aside class=\"admonition-where-to-get-help admonition\" role=\"note\">\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/4.0/faq/help/\"><span class=\"doc\">Getting Help</span></a> section of the FAQ.</p>\n</aside>\n<section id=\"creating-a-project\">\n<h2>Δημιουργία project<a class=\"heading-anchor\" href=\"#creating-a-project\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Αν αυτή είναι η πρώτη φορά που χρησιμοποιείτε το Django, θα χρειαστεί να ρυθμίσετε κάποια πράγματα στην αρχή. Θυμηθείτε ότι οι σωστές βάσεις δημιουργούν σταθερά σπίτια. Πιο συγκεκριμένα, θα χρειαστεί να τρέξετε μια Django εντολή η οποία θα δημιουργήσει αυτόματα ένα Django <a class=\"reference internal\" href=\"/el/4.0/glossary/#term-project\"><span class=\"xref std std-term\">project</span></a> – το οποίο δεν είναι τίποτε άλλο παρά ένας φάκελος (μια συλλογή από ρυθμίσεις, αν θέλετε) που περιέχει διάφορα αρχεία, όπως ρυθμίσεις της βάσης δεδομένων, διάφορες επιλογές του Django και ρυθμίσεις που αφορούν το application σας. Αν μπερδευτήκατε, μην ανησυχείτε, θα διαλευκανθούν όλα καθώς προχωράμε στον οδηγό.</p>\n<p>Από κονσόλα τρέξτε την εντολή, <code class=\"docutils literal notranslate\">cd</code> και τον φάκελο που θα θέλατε να αποθηκευτεί το Django project σας. Μετά τρέξτε την ακόλουθη εντολή:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-1-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>django-admin startproject mysite\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-1-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> django-admin startproject mysite\n</code></pre></div></div></div>\n<p>Συγχαρητήρια! Μόλις δημιουργήσατε το πρώτο σας Django project. Αυτή η εντολή θα δημιουργήσει αυτόματα (μέσα στον φάκελο που κάνατε <code class=\"docutils literal notranslate\">cd</code>) ένα φάκελο με το όνομα <code class=\"docutils literal notranslate\">mysite</code>. Αν δεν δούλεψε, δείτε στα <a class=\"reference internal\" href=\"/el/4.0/faq/troubleshooting/#troubleshooting-django-admin\"><span class=\"std std-ref\">προβλήματα τρέχοντας το django-admin</span></a>.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Σημείωση</p>\n<p>Θα πρέπει να δώσετε ιδιαίτερη προσοχή στην ονομασία του project σας, προσέχοντας να μην το ονομάσετε με δεσμευμένες λέξεις που χρησιμοποιεί η Python ή το Django. Πιο συγκεκριμένα, θα πρέπει να αποφεύγετε ονομασίες όπως <code class=\"docutils literal notranslate\">django</code> (κάτι το οποίο θα έρθει σε σύγκρουση με το ίδιο το Django) ή <code class=\"docutils literal notranslate\">test</code> (το οποίο θα έρθει σε σύγκρουση με το προεγκατεστημένο πακέτο της Python).</p>\n</aside>\n<aside class=\"admonition-where-should-this-code-live admonition\" role=\"note\">\n<p class=\"admonition-title\">Που να αποθηκεύσω το project μου;</p>\n<p>If your background is in plain old PHP (with no use of modern frameworks),\nyou’re probably used to putting code under the web server’s document root\n(in a place such as <code class=\"docutils literal notranslate\">/var/www</code>). With Django, you don’t do that. It’s\nnot a good idea to put any of this Python code within your web server’s\ndocument root, because it risks the possibility that people may be able\nto view your code over the web. That’s not good for security.</p>\n<p>Αντιθέτως, μπορείτε να βάλετε τον κώδικα σας σε κάποιον φάκελο <strong>έξω</strong> από το root φάκελο του Web server, όπως <code class=\"file docutils literal notranslate\">/home/mycode</code>.</p>\n</aside>\n<p>Ας δούμε τώρα τι δημιούργησε η εντολή <a class=\"reference internal\" href=\"/el/4.0/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\">startproject</code></a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</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=\"Code code\"><code>mysite<span class=\"o\">/</span>\n    manage<span class=\"o\">.</span>py\n    mysite<span class=\"o\">/</span>\n        <span class=\"fm\">__init__</span><span class=\"o\">.</span>py\n        settings<span class=\"o\">.</span>py\n        urls<span class=\"o\">.</span>py\n        asgi<span class=\"o\">.</span>py\n        wsgi<span class=\"o\">.</span>py\n</code></pre></div>\n<p>Αυτά τα αρχεία είναι:</p>\n<ul class=\"simple\">\n<li><p>The outer <code class=\"file docutils literal notranslate\">mysite/</code> root directory is a container for your project. Its\nname doesn’t matter to Django; you can rename it to anything you like.</p></li>\n<li><p><code class=\"file docutils literal notranslate\">manage.py</code>: Αυτό το αρχείο αποτελεί εντολή (κονσόλας) την οποία μπορείτε να χρησιμοποιήσετε (και θα το κάνετε) για να αλληλεπιδράτε με το project σας για διάφορους λόγους (π.χ δημιουργία πινάκων στη database, εκκίνηση του προ-εγκατεστημένου server κλπ). Μπορείτε να διαβάσετε όλες τις λεπτομέρειες σχετικά με το αρχείο <code class=\"file docutils literal notranslate\">manage.py</code> στο <a class=\"reference internal\" href=\"/el/4.0/ref/django-admin/\"><span class=\"doc\">django-admin and manage.py</span></a>.</p></li>\n<li><p>Ο εσωτερικός φάκελος <code class=\"file docutils literal notranslate\">mysite/</code> είναι ένα τυπικό Python package για το project σας. Το όνομα του (έχει σημασία) θα το χρησιμοποιήσετε για να κάνετε import οτιδήποτε σε άλλα Python packages (π.χ <code class=\"docutils literal notranslate\">import mysite.urls</code>).</p></li>\n<li><p><code class=\"file docutils literal notranslate\">mysite/__init__.py</code>: Ένα κενό αρχείο που λέει στην Python ότι αυτός ο φάκελος θα πρέπει να θεωρηθεί ως Python package. Αν είστε καινούργιος στην Python, διαβάστε <a class=\"reference external\" href=\"https://docs.python.org/3/tutorial/modules.html#tut-packages\" title=\"(στη Python έκδοση 3.14)\"><span class=\"xref std std-ref\">περισσότερα σχετικά με τα packages</span></a> στην επίσημη ιστοσελίδα της Python.</p></li>\n<li><p><code class=\"file docutils literal notranslate\">mysite/settings.py</code>: Ένα αρχείο που κρατά όλες τις ρυθμίσεις/παραμέτρους για αυτό το Django project (είναι η καρδιά ενός project).  Το άρθρο <a class=\"reference internal\" href=\"/el/4.0/topics/settings/\"><span class=\"doc\">Django settings</span></a> θα σας εξηγήσει όλα όσα χρειάζεστε σχετικά με το πως δουλεύουν οι ρυθμίσεις αυτές.</p></li>\n<li><p><code class=\"file docutils literal notranslate\">mysite/urls.py</code>: Το αρχείο αυτό κρατά τους ορισμούς των URLs για αυτό το project. Με άλλα λόγια είναι ένας «πίνακας περιεχομένων» του Django site σας. Μπορείτε να διαβάστε περισσότερα για τα URLs στο άρθρο <a class=\"reference internal\" href=\"/el/4.0/topics/http/urls/\"><span class=\"doc\">URL dispatcher</span></a>.</p></li>\n<li><p><code class=\"file docutils literal notranslate\">mysite/asgi.py</code>: An entry-point for ASGI-compatible web servers to\nserve your project. See <a class=\"reference internal\" href=\"/el/4.0/howto/deployment/asgi/\"><span class=\"doc\">How to deploy with ASGI</span></a> for more details.</p></li>\n<li><p><code class=\"file docutils literal notranslate\">mysite/wsgi.py</code>: Αυτό το αρχείο αποτελεί ένα σημείο εισόδου για τους Web servers οι οποίοι είναι συμβατοί με το WSGI specification. Δείτε περισσότερα στο άρθρο <a class=\"reference internal\" href=\"/el/4.0/howto/deployment/wsgi/\"><span class=\"doc\">πως να δουλέψω με το WSGI</span></a> για περισσότερες λεπτομέρειες.</p></li>\n</ul>\n</section>\n<section id=\"the-development-server\">\n<h2>Ο development server<a class=\"heading-anchor\" href=\"#the-development-server\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Τώρα που δημιουργήσαμε ένα Django project, θα πρέπει να σιγουρευτούμε ότι λειτουργεί. Από κονσόλα (γραμμή εντολών) μεταβείτε στο root φάκελο του project σας (<code class=\"file docutils literal notranslate\">mysite</code>) και τρέξτε την ακόλουθη εντολή:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-2-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 manage.py runserver\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-2-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>Θα δείτε την ακόλουθη έξοδο, στη κονσόλα:</p>\n<pre class=\"literal-block\">Performing system checks...\n\nSystem check identified no issues (0 silenced).\n\nYou have unapplied migrations; your app may not work properly until they are applied.\nRun 'python manage.py migrate' to apply them.\n\n09 Αυγούστου 2026 - 15:50:53\nDjango version 4.0, using settings 'mysite.settings'\nStarting development server at <a class=\"reference external\" href=\"http://127.0.0.1:8000/\">http://127.0.0.1:8000/</a>\nQuit the server with CONTROL-C.</pre>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Σημείωση</p>\n<p>Αγνοήστε την προειδοποίηση (warning) περί unapplied migrations προς το παρόν. Θα ασχοληθούμε με αυτό σε λίγο (αφορά την βάση δεδομένων).</p>\n</aside>\n<p>You’ve started the Django development server, a lightweight web server written\npurely in Python. We’ve included this with Django so you can develop things\nrapidly, without having to deal with configuring a production server – such as\nApache – until you’re ready for production.</p>\n<p>Now’s a good time to note: <strong>don’t</strong> use this server in anything resembling a\nproduction environment. It’s intended only for use while developing. (We’re in\nthe business of making web frameworks, not web servers.)</p>\n<p>Now that the server’s running, visit <a class=\"reference external\" href=\"http://127.0.0.1:8000/\">http://127.0.0.1:8000/</a> with your web\nbrowser. You’ll see a «Congratulations!» page, with a rocket taking off.\nIt worked!</p>\n<aside class=\"admonition-changing-the-port admonition\" role=\"note\">\n<p class=\"admonition-title\">Αλλαγή της πόρτας</p>\n<p>Από προεπιλογή, η εντολή <a class=\"reference internal\" href=\"/el/4.0/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\">runserver</code></a> ξεκινά έναν development server στην εσωτερική διεύθυνση IP που ακούει στην πόρτα 8000.</p>\n<p>Αν θελήσετε να αλλάξετε την πόρτα, δεν έχετε παρά να το περάσετε ως όρισμα στην εντολή. Για παράδειγμα, η παρακάτω εντολή ξεκινά τον server στην πόρτα 8080:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-3-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 manage.py runserver <span class=\"m\">8080</span>\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-3-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 8080\n</code></pre></div></div></div>\n<p>Αν θέλετε, επίσης, να αλλάξετε και την IP του server, εισάγετε τη νέα IP μαζί με την πόρτα. Για παράδειγμα, αν θέλετε ο server να ακούει σε όλες τις public IPs (χρήσιμο όταν χρησιμοποιείται το Vagrant ή όταν θέλετε να δείξετε τη δουλειά σας σε άλλους υπολογιστές στο δικό σας δίκτυο), χρησιμοποιήστε την ακόλουθη εντολή:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-4-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 manage.py runserver <span class=\"m\">0</span>:8000\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-4-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 0:8000\n</code></pre></div></div></div>\n<p>Το <strong>0</strong> είναι μια συντόμευση του <strong>0.0.0.0</strong>. Πλήρης τεκμηρίωση για τον development server μπορεί να βρεθεί στην αναφορά <a class=\"reference internal\" href=\"/el/4.0/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\">runserver</code></a>.</p>\n</aside>\n<aside class=\"admonition-automatic-reloading-of-djadmin-runserver admonition\" role=\"note\">\n<p class=\"admonition-title\">Αυτόματη ανανέωση του <a class=\"reference internal\" href=\"/el/4.0/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\">runserver</code></a></p>\n<p>Ο development server ανανεώνεται (reloads) αυτόματα κάθε φορά που αποθηκεύετε ή αλλάζετε κάποιο αρχείο (φυσικά μέσα στο project σας). Με αυτό τον τρόπο δεν χρειάζεται να κάνετε χειροκίνητα το reload του server για να δεχτεί τυχόν αλλαγές που πραγματοποιήθηκαν. Ωστόσο, υπάρχουν περιπτώσεις που ο server δεν ανανεώνεται αυτόματα, όπως στην προσθήκη ή αφαίρεση κάποιου αρχείου. Εκεί πρέπει να γίνει χειροκίνητα η επαναφόρτωση του server.</p>\n</aside>\n</section>\n<section id=\"creating-the-polls-app\">\n<h2>Δημιουργώντας το app ψηφοφορίας<a class=\"heading-anchor\" href=\"#creating-the-polls-app\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Τώρα που το περιβάλλον εργασίας σας – το «project» – είναι έτοιμο, μπορείτε να ξεκινήσετε να δημιουργείτε.</p>\n<p>Κάθε application που γράφετε στο Django αποτελείται από ένα Python package (ένας φάκελος που περιέχει το αρχείο <code class=\"docutils literal notranslate\">__init__.py</code>) το οποίο πρέπει να ακολουθεί ορισμένους κανόνες. Για άλλη μια φορά, το Django σας παρέχει την ανάλογη εντολή για να δημιουργήσετε αυτόματα τη δομή αυτού του φακέλου (Python package) ούτως ώστε να επικεντρωθείτε στο να γράψετε κώδικα παρά στο να δημιουργείτε φακέλους και υποφακέλους.</p>\n<aside class=\"admonition-projects-vs-apps admonition\" role=\"note\">\n<p class=\"admonition-title\">Projects vs. apps</p>\n<p>What’s the difference between a project and an app? An app is a web\napplication that does something – e.g., a blog system, a database of\npublic records or a small poll app. A project is a collection of\nconfiguration and apps for a particular website. A project can contain\nmultiple apps. An app can be in multiple projects.</p>\n</aside>\n<p>Your apps can live anywhere on your <a class=\"reference external\" href=\"https://docs.python.org/3/tutorial/modules.html#tut-searchpath\" title=\"(στη Python έκδοση 3.14)\"><span class=\"xref std std-ref\">Python path</span></a>. In\nthis tutorial, we’ll create our poll app in the same directory as your\n<code class=\"file docutils literal notranslate\">manage.py</code> file so that it can be imported as its own top-level module,\nrather than a submodule of <code class=\"docutils literal notranslate\">mysite</code>.</p>\n<p>Για να δημιουργήσετε το app, σιγουρευτείτε ότι βρίσκεστε στο ίδιο επίπεδο με το αρχείο <code class=\"file docutils literal notranslate\">manage.py</code> και πληκτρολογήστε την εντολή:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-5-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 manage.py startapp polls\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-5-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 startapp polls\n</code></pre></div></div></div>\n<p>Συγχαρητήρια και πάλι! Μόλις φτιάξατε την πρώτη σας εφαρμογή στο Django. Βέβαια δεν κάνει τίποτα ακόμα αλλά θα φτάσουμε και εκεί. Η εντολή θα δημιουργήσει ένα φάκελο <code class=\"file docutils literal notranslate\">polls</code>, ο οποίος αναπτύσσεται ως εξής:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</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=\"Code code\"><code>polls<span class=\"o\">/</span>\n    <span class=\"fm\">__init__</span><span class=\"o\">.</span>py\n    admin<span class=\"o\">.</span>py\n    apps<span class=\"o\">.</span>py\n    migrations<span class=\"o\">/</span>\n        <span class=\"fm\">__init__</span><span class=\"o\">.</span>py\n    models<span class=\"o\">.</span>py\n    tests<span class=\"o\">.</span>py\n    views<span class=\"o\">.</span>py\n</code></pre></div>\n<p>Αυτή η δομή θα εσωκλείει ολόκληρη την εφαρμογή μας.</p>\n</section>\n<section id=\"write-your-first-view\">\n<h2>Γράφοντας το πρώτο σας view<a class=\"heading-anchor\" href=\"#write-your-first-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Ας γράψουμε το πρώτο μας view. Ανοίξτε το αρχείο <code class=\"docutils literal notranslate\">polls/views.py</code> και γράψτε τον ακόλουθο Python κώδικα:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">polls/views.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.http</span> <span class=\"kn\">import</span> HttpResponse\n\n\n<span class=\"k\">def</span> <span class=\"nf\">index</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    <span class=\"k\">return</span> HttpResponse<span class=\"p\">(</span><span class=\"s2\">&quot;Hello, world. You&#39;re at the polls index.&quot;</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>Αυτή είναι η πιο απλή μορφή view στο Django. Για να καλέσετε το view, θα πρέπει να το συσχετίσουμε με ένα URL - και γι” αυτό θα χρειαστούμε ένα URLconf.</p>\n<p>Για τη δημιουργία του URLconf στον φάκελο polls, δημιουργήστε ένα αρχείο με το όνομα <code class=\"docutils literal notranslate\">urls.py</code>. Ο φάκελος της εφαρμογής σας θα πρέπει να δείχνει έτσι:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</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=\"Code code\"><code>polls<span class=\"o\">/</span>\n    <span class=\"fm\">__init__</span><span class=\"o\">.</span>py\n    admin<span class=\"o\">.</span>py\n    apps<span class=\"o\">.</span>py\n    migrations<span class=\"o\">/</span>\n        <span class=\"fm\">__init__</span><span class=\"o\">.</span>py\n    models<span class=\"o\">.</span>py\n    tests<span class=\"o\">.</span>py\n    urls<span class=\"o\">.</span>py\n    views<span class=\"o\">.</span>py\n</code></pre></div>\n<p>Μέσα στο αρχείο <code class=\"docutils literal notranslate\">polls/urls.py</code> συμπεριλάβετε τον παρακάτω κώδικα:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">polls/urls.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> path\n\n<span class=\"kn\">from</span> <span class=\"nn\">.</span> <span class=\"kn\">import</span> views\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">,</span> views<span class=\"o\">.</span>index<span class=\"p\">,</span> name<span class=\"o\">=</span><span class=\"s1\">&#39;index&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>The next step is to point the root URLconf at the <code class=\"docutils literal notranslate\">polls.urls</code> module. In\n<code class=\"docutils literal notranslate\">mysite/urls.py</code>, add an import for <code class=\"docutils literal notranslate\">django.urls.include</code> and insert an\n<a class=\"reference internal\" href=\"/el/4.0/ref/urls/#django.urls.include\" title=\"django.urls.include\"><code class=\"xref py py-func docutils literal notranslate\">include()</code></a> in the <code class=\"docutils literal notranslate\">urlpatterns</code> list, so you have:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">mysite/urls.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</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=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib</span> <span class=\"kn\">import</span> admin\n<span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> include<span class=\"p\">,</span> path\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;polls/&#39;</span><span class=\"p\">,</span> include<span class=\"p\">(</span><span class=\"s1\">&#39;polls.urls&#39;</span><span class=\"p\">)),</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;admin/&#39;</span><span class=\"p\">,</span> admin<span class=\"o\">.</span>site<span class=\"o\">.</span>urls<span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>The <a class=\"reference internal\" href=\"/el/4.0/ref/urls/#django.urls.include\" title=\"django.urls.include\"><code class=\"xref py py-func docutils literal notranslate\">include()</code></a> function allows referencing other URLconfs.\nWhenever Django encounters <a class=\"reference internal\" href=\"/el/4.0/ref/urls/#django.urls.include\" title=\"django.urls.include\"><code class=\"xref py py-func docutils literal notranslate\">include()</code></a>, it chops off whatever\npart of the URL matched up to that point and sends the remaining string to the\nincluded URLconf for further processing.</p>\n<p>The idea behind <a class=\"reference internal\" href=\"/el/4.0/ref/urls/#django.urls.include\" title=\"django.urls.include\"><code class=\"xref py py-func docutils literal notranslate\">include()</code></a> is to make it easy to\nplug-and-play URLs. Since polls are in their own URLconf\n(<code class=\"docutils literal notranslate\">polls/urls.py</code>), they can be placed under «/polls/», or under\n«/fun_polls/», or under «/content/polls/», or any other path root, and the\napp will still work.</p>\n<aside class=\"admonition-when-to-use-func-django-urls-include admonition\" role=\"note\">\n<p class=\"admonition-title\">When to use <a class=\"reference internal\" href=\"/el/4.0/ref/urls/#django.urls.include\" title=\"django.urls.include\"><code class=\"xref py py-func docutils literal notranslate\">include()</code></a></p>\n<p>Θα πρέπει πάντα να χρησιμοποιείτε στην <code class=\"docutils literal notranslate\">include()</code> όταν θέλετε να περιλάβετε άλλα URL patterns. Το <code class=\"docutils literal notranslate\">admin.site.urls</code> είναι η μόνη εξαίρεση σε αυτό τον κανόνα.</p>\n</aside>\n<p>You have now wired an <code class=\"docutils literal notranslate\">index</code> view into the URLconf. Verify it’s working with\nthe following command:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-6-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 manage.py runserver\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-6-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>Επισκεφτείτε την σελίδα <a class=\"reference external\" href=\"http://localhost:8000/polls/\">http://localhost:8000/polls/</a> με τον browser σας και θα πρέπει να δείτε το κείμενο «<em>Hello, world. You’re at the polls index.</em>», το οποίο γράψαμε στην <code class=\"docutils literal notranslate\">index</code> view.</p>\n<aside class=\"admonition-page-not-found admonition\" role=\"note\">\n<p class=\"admonition-title\">Page not found?</p>\n<p>If you get an error page here, check that you’re going to\n<a class=\"reference external\" href=\"http://localhost:8000/polls/\">http://localhost:8000/polls/</a> and not <a class=\"reference external\" href=\"http://localhost:8000/\">http://localhost:8000/</a>.</p>\n</aside>\n<p>The <a class=\"reference internal\" href=\"/el/4.0/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\">path()</code></a> function is passed four arguments, two required:\n<code class=\"docutils literal notranslate\">route</code> and <code class=\"docutils literal notranslate\">view</code>, and two optional: <code class=\"docutils literal notranslate\">kwargs</code>, and <code class=\"docutils literal notranslate\">name</code>.\nAt this point, it’s worth reviewing what these arguments are for.</p>\n<section id=\"path-argument-route\">\n<h3><a class=\"reference internal\" href=\"/el/4.0/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\">path()</code></a> argument: <code class=\"docutils literal notranslate\">route</code><a class=\"heading-anchor\" href=\"#path-argument-route\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><code class=\"docutils literal notranslate\">route</code> is a string that contains a URL pattern. When processing a request,\nDjango starts at the first pattern in <code class=\"docutils literal notranslate\">urlpatterns</code> and makes its way down\nthe list, comparing the requested URL against each pattern until it finds one\nthat matches.</p>\n<p>Patterns don’t search GET and POST parameters, or the domain name. For example,\nin a request to <code class=\"docutils literal notranslate\">https://www.example.com/myapp/</code>, the URLconf will look for\n<code class=\"docutils literal notranslate\">myapp/</code>. In a request to <code class=\"docutils literal notranslate\">https://www.example.com/myapp/?page=3</code>, the\nURLconf will also look for <code class=\"docutils literal notranslate\">myapp/</code>.</p>\n</section>\n<section id=\"path-argument-view\">\n<h3><a class=\"reference internal\" href=\"/el/4.0/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\">path()</code></a> argument: <code class=\"docutils literal notranslate\">view</code><a class=\"heading-anchor\" href=\"#path-argument-view\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When Django finds a matching pattern, it calls the specified view function with\nan <a class=\"reference internal\" href=\"/el/4.0/ref/request-response/#django.http.HttpRequest\" title=\"django.http.HttpRequest\"><code class=\"xref py py-class docutils literal notranslate\">HttpRequest</code></a> object as the first argument and any\n«captured» values from the route as keyword arguments. We’ll give an example\nof this in a bit.</p>\n</section>\n<section id=\"path-argument-kwargs\">\n<h3><a class=\"reference internal\" href=\"/el/4.0/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\">path()</code></a> argument: <code class=\"docutils literal notranslate\">kwargs</code><a class=\"heading-anchor\" href=\"#path-argument-kwargs\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Επίσης, στο Django view μπορούν να περαστούν, προαιρετικά και αυθαίρετα keyword arguments. Σε αυτό τον οδηγό δεν θα χρησιμοποιήσουμε αυτή τη δυνατότητα.</p>\n</section>\n<section id=\"path-argument-name\">\n<h3><a class=\"reference internal\" href=\"/el/4.0/ref/urls/#django.urls.path\" title=\"django.urls.path\"><code class=\"xref py py-func docutils literal notranslate\">path()</code></a> argument: <code class=\"docutils literal notranslate\">name</code><a class=\"heading-anchor\" href=\"#path-argument-name\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Υπάρχει επίσης το πολύ δυνατό χαρακτηριστικό του Django, με το οποίο μπορείτε να ονομάσετε τα URL σας και να αναφέρεστε σε αυτά μόνο με το όνομα τους παρά με την διεύθυνση τους. Αυτό, θα το χρησιμοποιείτε συνέχεια στα templates σας (π.χ μέσα σε ένα <code class=\"docutils literal notranslate\">&lt;a href&gt;</code> HTML tag). Με αυτό τον τρόπο μπορείτε να αλλάζετε τις διευθύνσεις των URL πειράζοντας μόνο ένα αρχείο (<code class=\"docutils literal notranslate\">urls.py</code>) παρά να ψάχνετε σε άλλα μέρη (π.χ στα template σας) για να ανανεώσετε το URL (αφού, αλλιώς θα είχατε γράψει ολόκληρη τη διεύθυνση URL).</p>\n<p>Όταν αισθανθείτε άνετα με αυτό τον οδηγό και με τη λειτουργία της Web request-Web response διαδικασίας μπορείτε να συνεχίσετε στο <a class=\"reference internal\" href=\"/el/4.0/intro/tutorial02/\"><span class=\"doc\">δεύτερο μέρος αυτού του οδηγού</span></a> για να εργαστείτε με τη βάση δεδομένων.</p>\n</section>\n</section>","rootId":"writing-your-first-django-app-part-1","toc":[{"title":"Δημιουργία project","anchor":"creating-a-project","children":[]},{"title":"Ο development server","anchor":"the-development-server","children":[]},{"title":"Δημιουργώντας το app ψηφοφορίας","anchor":"creating-the-polls-app","children":[]},{"title":"Γράφοντας το πρώτο σας view","anchor":"write-your-first-view","children":[{"title":"path() argument: route","anchor":"path-argument-route","children":[]},{"title":"path() argument: view","anchor":"path-argument-view","children":[]},{"title":"path() argument: kwargs","anchor":"path-argument-kwargs","children":[]},{"title":"path() argument: name","anchor":"path-argument-name","children":[]}]}],"breadcrumbs":[{"docname":"intro/index","title":"Ξεκινώντας","url":"/el/4.0/intro/"}],"prev":{"docname":"intro/install","title":"Οδηγός γρήγορης εγκατάστασης","url":"/el/4.0/intro/install/"},"next":{"docname":"intro/tutorial02","title":"Γράφοντας το πρώτο σας Django app, μέρος 2","url":"/el/4.0/intro/tutorial02/"},"formats":{"html":"/el/4.0/intro/tutorial01/","markdown":"/el/4.0/intro/tutorial01.md","json":"/el/4.0/intro/tutorial01.json"},"source":"https://github.com/django/django/blob/stable/4.0.x/docs/intro/tutorial01.txt","official":"https://docs.djangoproject.com/el/4.0/intro/tutorial01/","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","it","pt-br","ko","es","el","pl"]}