-
Notifications
You must be signed in to change notification settings - Fork 2.8k
收藏系统 #766
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
收藏系统 #766
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Generated by Django 5.2.1 on 2025-05-17 10:30 | ||
|
|
||
| import django.db.models.deletion | ||
| import django.utils.timezone | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("blog", "0006_alter_blogsettings_options"), | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="Favorite", | ||
| fields=[ | ||
| ("id", models.AutoField(primary_key=True, serialize=False)), | ||
| ( | ||
| "creation_time", | ||
| models.DateTimeField( | ||
| default=django.utils.timezone.now, verbose_name="creation time" | ||
| ), | ||
| ), | ||
| ( | ||
| "last_modify_time", | ||
| models.DateTimeField( | ||
| default=django.utils.timezone.now, verbose_name="modify time" | ||
| ), | ||
| ), | ||
| ( | ||
| "article", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| to="blog.article", | ||
| verbose_name="article", | ||
| ), | ||
| ), | ||
| ( | ||
| "user", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| to=settings.AUTH_USER_MODEL, | ||
| verbose_name="user", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "verbose_name": "favorite", | ||
| "verbose_name_plural": "favorite", | ||
| "unique_together": {("user", "article")}, | ||
| }, | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||
| {% extends 'share_layout/base.html' %} | ||||||
| {% load blog_tags %} | ||||||
| {% load static %} | ||||||
|
|
||||||
| {% block header %} | ||||||
| <title>{{ article.title }} | {{ SITE_DESCRIPTION }}</title> | ||||||
|
|
@@ -32,6 +33,15 @@ | |||||
| <div id="content" role="main"> | ||||||
| {% load_article_detail article False user %} | ||||||
|
|
||||||
| {% if user.is_authenticated %} | ||||||
| <div class="favorite-container" style="margin: 20px 0;"> | ||||||
| <button id="favoriteBtn" class="btn btn-primary" data-article-id="{{ article.id }}"> | ||||||
| <i class="fa fa-star"></i> | ||||||
| <span id="favoriteText">收藏文章</span> | ||||||
|
||||||
| <span id="favoriteText">收藏文章</span> | |
| <span id="favoriteText">{% trans "收藏文章" %}</span> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| {% extends 'share_layout/base.html' %} | ||
| {% load blog_tags %} | ||
| {% load i18n %} | ||
| {% load static %} | ||
|
|
||
| {% block title %} | ||
| 我的收藏 - {{ SITE_NAME }} | ||
| {% endblock %} | ||
|
|
||
| {% block content %} | ||
| <div class="container"> | ||
| <div class="row"> | ||
| <div class="col-md-8"> | ||
| <div class="card"> | ||
| <div class="card-header"> | ||
| <h3 class="card-title">我的收藏</h3> | ||
| </div> | ||
| <div class="card-body"> | ||
| {% if favorites %} | ||
| <div class="article-list"> | ||
| {% for favorite in favorites %} | ||
| <div class="article-item"> | ||
| <h2 class="article-title"> | ||
| <a href="{{ favorite.article.get_absolute_url }}"> | ||
| {{ favorite.article.title }} | ||
| </a> | ||
| </h2> | ||
| <div class="article-meta"> | ||
| <span class="article-date"> | ||
| 收藏于: {{ favorite.creation_time|date:"Y-m-d H:i" }} | ||
| </span> | ||
| <span class="article-category"> | ||
| 分类: <a href="{{ favorite.article.category.get_absolute_url }}"> | ||
| {{ favorite.article.category.name }} | ||
| </a> | ||
| </span> | ||
| <button class="btn btn-sm btn-danger remove-favorite" | ||
| data-article-id="{{ favorite.article.id }}"> | ||
| 取消收藏 | ||
| </button> | ||
| </div> | ||
| </div> | ||
| {% endfor %} | ||
| </div> | ||
| {% else %} | ||
| <p class="text-center">还没有收藏任何文章</p> | ||
| {% endif %} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| {% endblock %} | ||
|
|
||
| {% block extra_js %} | ||
| <script src="{% static 'blog/js/blog.js' %}"></script> | ||
| {% endblock %} |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,12 @@ | |||||
| class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-3498"> | ||||||
| <a href="/">{% trans 'index' %}</a></li> | ||||||
|
|
||||||
| <li class="menu-item"> | ||||||
| <a href="{% url 'blog:favorite_list' %}"> | ||||||
| <i class="fa fa-star"></i> 我的收藏 | ||||||
|
||||||
| <i class="fa fa-star"></i> 我的收藏 | |
| <i class="fa fa-star"></i> {% trans '我的收藏' %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding error handling (e.g. catch blocks) for fetch requests will make the client-side interactions more robust in case of network failures.