Skip to content

Fix bugs in backend logic, database logic; Add features #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified mysite/db.sqlite3
Binary file not shown.
2 changes: 1 addition & 1 deletion mysite/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Create your models here.
class ToDoList(models.Model):
date = models.DateTimeField("date published")
date = models.DateTimeField("date published", auto_now_add=True)
name = models.CharField(max_length=200)

def __str__(self):
Expand Down
82 changes: 58 additions & 24 deletions mysite/main/templates/main/index.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,65 @@
{#{% extends 'main/base.html' %}#}
{##}
{#{% block title %}View List{% endblock %}#}
{##}
{#{% block content %}#}
{# <h2>{{ls.name}}</h2>#}
{# <form method="post", action="#">#}
{# {% csrf_token %}#}
{##}
{# {% for item in ls.item_set.all%}#}
{# <div class="input-group mb-3">#}
{# <div class="input-group-prepend">#}
{# <div class="input-group-text">#}
{# <input type="checkbox" name="c{{item.id}}" value="clicked" aria-label="Checkbox for following text input" {% if item.complete %} checked {% endif %}>#}
{# </div>#}
{# </div>#}
{# <input type="text" name="text{{item.id}}" value="{{item.text}}" class="form-control" aria-label="Text input with checkbox">#}
{# </div>#}
{# {% endfor %}#}
{##}
{# <div class="input-group mb-3">#}
{# <div class="input-group-prepend">#}
{# <button name="add", value="add", type="submit", class="btn btn-success">Add New</button>#}
{# </div>#}
{# <input type="text" name="new" value="" class="form-control">#}
{# #}
{# </div>#}
{# <br>#}
{# <button name="save", value="save", type="submit", class="btn btn-success">Save</button>#}
{# </form>#}
{#{% endblock %}#}

{% extends 'main/base.html' %}

{% block title %}View List{% endblock %}

{% block content %}
<h2>{{ls.name}}</h2>
<form method="post", action="#">
{% csrf_token %}
<h2>{{ls.name}}</h2>
<form method="post", action="#">
{% csrf_token %}

{% for item in ls.item_set.all%}
<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
<input type="checkbox" name="c{{item.id}}" value="clicked" aria-label="Checkbox for following text input" {% if item.complete %} checked {% endif %}>
</div>
</div>
<input type="text" name="text{{item.id}}" value="{{item.text}}" class="form-control" aria-label="Text input with checkbox">
</div>
{% endfor %}
{% for item in ls.item_set.all%}
<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
<input type="checkbox" name="c{{item.id}}" value="clicked" aria-label="Checkbox for following text input" {% if item.complete %} checked {% endif %}>
</div>
</div>
<input type="text" name="text{{item.id}}" value="{{item.text}}" class="form-control" aria-label="Text input with checkbox">
<div class="input-group-append">
<button name="delete" value="{{item.id}}" type="submit" class="btn btn-danger">Delete</button>
</div>
</div>
{% endfor %}

<div class="input-group mb-3">
<div class="input-group-prepend">
<button name="add", value="add", type="submit", class="btn btn-success">Add New</button>
</div>
<input type="text" name="new" value="" class="form-control">

</div>
<br>
<button name="save", value="save", type="submit", class="btn btn-success">Save</button>
</form>
{% endblock %}
<div class="input-group mb-3">
<div class="input-group-prepend">
<button name="add", value="add", type="submit", class="btn btn-success">Add New</button>
</div>
<input type="text" name="new" value="" class="form-control">
</div>
<br>
<button name="save", value="save", type="submit", class="btn btn-success">Save</button>
</form>
{% endblock %}
87 changes: 76 additions & 11 deletions mysite/main/templates/main/view.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,81 @@
{#{% extends 'main/base.html' %}#}
{##}
{#{% block title %}View All Lists{% endblock %}#}
{##}
{#{% block content %}#}
{# <div class="container mt-4">#}
{# {% if user.is_authenticated %}#}
{# <h2 class="mb-4 text-primary">My list items</h2>#}
{# {% if lists %}#}
{# <div class="list-group shadow-sm">#}
{# {% for l in lists %}#}
{# <a href="/{{l.id}}" class="list-group-item list-group-item-action">#}
{# <h3 class="mb-1">{{l.name}}</h3>#}
{# </a>#}
{# {% endfor %}#}
{# </div>#}
{# {% else %}#}
{# <div class="alert alert-info">#}
{# You haven't created any lists yet.#}
{# </div>#}
{# {% endif %}#}
{# {% else %}#}
{# <div class="card border-0 shadow-sm p-4 text-center">#}
{# <div class="card-body">#}
{# <h3 class="card-title mb-4">Welcome to the list management system</h3>#}
{# <p class="card-text mb-4">Please log in or register to start using our services</p>#}
{# <div class="d-flex justify-content-center gap-3">#}
{# <a href="/login" class="btn btn-outline-primary btn-lg">Login</a>#}
{# <div style="width: 30px"></div>#}
{# <a href="/register" class="btn btn-outline-primary btn-lg">Register</a>#}
{# </div>#}
{# </div>#}
{# </div>#}
{# {% endif %}#}
{# </div>#}
{#{% endblock %}#}


{% extends 'main/base.html' %}

{% block title %}View All Lists{% endblock %}


{% block content %}
{% if user.is_authenticated %}
<ul class="list-group">
{% for l in lists %}
<li class="list-group-item"><a href="/{{l.id}}"><h3>{{l.name}}</h3></a></li>
{% endfor %}
</ul>
{% else %}
<h3>Login <a href="/login">Here</a></h3>
{% endif %}
{% endblock %}
<div class="container mt-4">
{% if user.is_authenticated %}
<h2 class="mb-4 text-primary">My list items</h2>
{% if lists %}
<div class="list-group shadow-sm">
{% for l in lists %}
<div class="list-group-item d-flex justify-content-between align-items-center">
<a href="/{{l.id}}" class="flex-grow-1">
<h3 class="mb-1">{{l.name}}</h3>
</a>
<form method="POST" action="{% url 'delete_list' l.id %}" class="ms-2">
{% csrf_token %}
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</div>
{% endfor %}
</div>
{% else %}
<div class="alert alert-info">
You haven't created any lists yet.
</div>
{% endif %}
{% else %}
<div class="card border-0 shadow-sm p-4 text-center">
<div class="card-body">
<h3 class="card-title mb-4">Welcome to the list management system</h3>
<p class="card-text mb-4">Please log in or register to start using our services</p>
<div class="d-flex justify-content-center gap-3">
<a href="/login" class="btn btn-outline-primary btn-lg">Login</a>
<div style="width: 30px"></div>
<a href="/register" class="btn btn-outline-primary btn-lg">Register</a>
</div>
</div>
</div>
{% endif %}
</div>
{% endblock %}

2 changes: 2 additions & 0 deletions mysite/main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
path("home/", views.home, name="home"),
path("create/", views.get_name, name="index"),
path("<int:id>", views.index, name="index"),
path('delete/<int:id>/', views.delete_list, name='delete_list'),

]
13 changes: 13 additions & 0 deletions mysite/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def index(request, id):
else:
print("invalid")

elif "delete" in request.POST:
item_id = int(request.POST["delete"])
item = ls.item_set.get(id=item_id)
item.delete()

return render(request, "main/index.html", {"ls": ls})


Expand Down Expand Up @@ -58,3 +63,11 @@ def home(request):
def view(request):
l = ToDoList.objects.all()
return render(request, "main/view.html", {"lists":l})

def delete_list(request, id):
if request.method == "POST":
ls = get_object_or_404(ToDoList, id=id)
ls.delete()
return HttpResponseRedirect("/view") # 重定向到清单列表页面
return HttpResponseRedirect("/view") # 如果不是POST请求,也重定向到列表页面

1 change: 1 addition & 0 deletions mysite/mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'main.apps.MainConfig',
"register.apps.RegisterConfig",
"crispy_forms",
'crispy_bootstrap4'
]

AUTH_USER_MODEL = "register.CustomUser"
Expand Down
2 changes: 2 additions & 0 deletions mysite/mysite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
from django.contrib import admin
from django.urls import path, include
from register import views as v
from django.contrib.auth import views as auth_views

urlpatterns = [
path('admin/', admin.site.urls),
path("register/", v.register, name="register"),
path('logout/', auth_views.LogoutView.as_view(next_page='/'), name='logout'),
path('', include("main.urls")),
path('', include("django.contrib.auth.urls"))
]
Expand Down
94 changes: 47 additions & 47 deletions mysite/register/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
# Generated by Django 2.2 on 2019-04-29 15:20
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0011_update_proxy_permissions'),
('main', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='CustomUser',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('toDoLists', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='main.ToDoList')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]
# Generated by Django 5.2.1 on 2025-05-21 16:37

import django.contrib.auth.models
import django.contrib.auth.validators
import django.db.models.deletion
import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
('main', '__first__'),
]

operations = [
migrations.CreateModel(
name='CustomUser',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
('toDoLists', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='main.todolist')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]
2 changes: 1 addition & 1 deletion mysite/register/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

# Create your models here.
class CustomUser(AbstractUser):
toDoLists = models.ForeignKey(ToDoList, on_delete=models.CASCADE)
pass
3 changes: 3 additions & 0 deletions mysite/register/templates/register/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
{% csrf_token %}
<legend class="border-bottom mb-3">Create an Account</legend>
{{form|crispy}}
<div class="d-flex align-items-center justify-content-between mt-3">
<button name="create", type="submit", class="btn btn-success">Register</button>
<span>Already have an account? <a href="{% url 'login' %}">Login</a></span>
</div>
</form>
</div>
{% endblock %}
Loading