From e8c0c8512e42c630a420d486e4bc84e433674313 Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Mon, 18 Dec 2023 15:41:12 +0500 Subject: [PATCH 1/9] Starting a new branch. Changed names of variables in the views file. Started the process of adding AJAX scripts in order to make the work with the blog smoother. --- README.md | 9 +++++++ server/my_app/urls.py | 1 + server/my_app/views.py | 22 ++++++++++++--- server/static/js/script.js | 10 +++++++ server/templates/base.html | 5 ++++ server/templates/home.html | 55 +++++++++++++++++++++++++++----------- 6 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 README.md create mode 100644 server/static/js/script.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..57bbf98 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# django-postgresql study +A simple blog project, meant to be proof of basic competence in working with the technologies of Django and PostgreSQL. + +There are two intended ways of starting the server. +First, one should navigate tho the `server/` subdirectory of the project, and then executing one of the following commands: +- to start the server directly, use the command `python manage.py runserver`. +- to start the server using Docker, use the command `docker-compose up [--build]`. + +Currently, the project allows a user to register, log in, post comments, and view comments. The access to this functionality is provided by the links in the header of the project's pages. \ No newline at end of file diff --git a/server/my_app/urls.py b/server/my_app/urls.py index 0eab88a..6bada98 100644 --- a/server/my_app/urls.py +++ b/server/my_app/urls.py @@ -12,5 +12,6 @@ path(r'home/create', views.create_post, name='post_create'), path(r'home/edit//', views.edit_post, name='post_edit'), path(r'home/delete//', views.delete_post, name='post_delete'), + path(r'home/post_create_norefresh', views.create_post_norefresh, name='post_create_norefresh'), path(r'', views.default_view, name='default') ] \ No newline at end of file diff --git a/server/my_app/views.py b/server/my_app/views.py index 6f30710..777adbd 100644 --- a/server/my_app/views.py +++ b/server/my_app/views.py @@ -102,8 +102,8 @@ def create_post(request): elif request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): - user = form.save(commit=False) - user.author = request.user + post = form.save(commit=False) + post.author = request.user form.save() messages.success(request, 'The post has been created successfully.') return redirect('home') @@ -111,6 +111,20 @@ def create_post(request): messages.error(request, 'Please correct the following errors:') return render(request, 'post_form.html', {'form': form}) + +@login_required +def create_post_norefresh(request): + if request.method == 'POST': + title = request.POST['title'] + content = request.POST['content'] + author = request.user + + new_post = Post(title=title, content=content, author=author) + new_post.save() + success = 'New comment created successfully' + messages.success(request, success) + return HttpResponse(success) + @login_required def edit_post(request, id): @@ -155,8 +169,8 @@ def home(request): elif request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): - user = form.save(commit=False) - user.author = request.user + post = form.save(commit=False) + post.author = request.user form.save() messages.success(request, 'The post has been created successfully.') return render(request, 'home.html', context) diff --git a/server/static/js/script.js b/server/static/js/script.js new file mode 100644 index 0000000..dec3bfb --- /dev/null +++ b/server/static/js/script.js @@ -0,0 +1,10 @@ +function dummy() { + var xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + document.getElementsByTagName("main").innerHTML = this.responseText; + } + }; + xhttp.open("POST", "home", true); + xhttp.send(); +} \ No newline at end of file diff --git a/server/templates/base.html b/server/templates/base.html index 9172aaa..478857a 100644 --- a/server/templates/base.html +++ b/server/templates/base.html @@ -5,6 +5,7 @@ My site + + +{% block script %} +{% endblock script %} +
{% if request.user.is_authenticated %} diff --git a/server/templates/home.html b/server/templates/home.html index f141318..fcb35fd 100644 --- a/server/templates/home.html +++ b/server/templates/home.html @@ -3,22 +3,47 @@ {% block style %} {% endblock style %} +{% block script %} + +{% endblock script %} + {% block content %} -

Create Post

-
- {% csrf_token %} - {{ form.as_p }} - -
+{% if request.user.is_authenticated %} +

Create Post

+
+ {% csrf_token %} + {{ form.as_p }} + {% comment %} {% endcomment %} + +
+ {% endif %}

My Posts

- {% for post in posts %} -

{{ post.title }}

- Published on {{ post.published_at | date:"M d, Y" }} by {{ post.author | title}} -

{{ post.content }}

+
+ {% for post in posts %} +

{{ post.title }}

+ Published on {{ post.published_at | date:"M d, Y" }} by {{ post.author | title}} +

{{ post.content }}

- {% if request.user.is_authenticated and request.user == post.author %} -

Edit

-

Delete

- {% endif %} - {% endfor %} + {% if request.user.is_authenticated and request.user == post.author %} +

Edit

+

Delete

+ {% endif %} + {% endfor %} +
{% endblock content %} \ No newline at end of file From f3ed85de7a0b45dd2b25554f13cb7cc639bb56f2 Mon Sep 17 00:00:00 2001 From: lcgeneralprojects Date: Tue, 20 Feb 2024 19:14:19 +0500 Subject: [PATCH 2/9] * Made it so that posting a comment from the /home/ url is done without refreshing the whole page * Fixed the issues regarding the interface being inaccessible to the user when working through Docker --- .idea/misc.xml | 3 ++ README.md | 2 +- server/.dockerignore | 1 + server/django_postgresql_study/settings.py | 3 +- server/docker-compose.yml | 26 +++++++++++++- server/dockerfile | 18 +++++++--- server/my_app/urls.py | 2 +- server/my_app/views.py | 40 ++++++++++++++++------ server/requirements.txt | 2 ++ server/templates/base.html | 1 + server/templates/home.html | 24 +++++++++++-- 11 files changed, 101 insertions(+), 21 deletions(-) create mode 100644 server/.dockerignore diff --git a/.idea/misc.xml b/.idea/misc.xml index 2446bb2..dbc5b9b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,8 @@ + +