-
Notifications
You must be signed in to change notification settings - Fork 2.9k
收藏系统 #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
Merged
Merged
收藏系统 #766
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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")}, | ||
| }, | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| {% extends 'share_layout/base.html' %} | ||
| {% load blog_tags %} | ||
| {% load i18n %} | ||
|
|
||
| {% 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> | ||
|
|
||
| <script> | ||
| document.addEventListener('DOMContentLoaded', function() { | ||
| document.querySelectorAll('.remove-favorite').forEach(button => { | ||
| button.addEventListener('click', function() { | ||
| const articleId = this.dataset.articleId; | ||
| fetch(`/favorite/remove/${articleId}/`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'X-CSRFToken': getCookie('csrftoken') | ||
| } | ||
| }) | ||
| .then(response => response.json()) | ||
| .then(data => { | ||
| if (data.status === 'success') { | ||
| this.closest('.article-item').remove(); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| // 获取CSRF Token的函数 | ||
| function getCookie(name) { | ||
Inkwhs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let cookieValue = null; | ||
| if (document.cookie && document.cookie !== '') { | ||
| const cookies = document.cookie.split(';'); | ||
| for (let i = 0; i < cookies.length; i++) { | ||
| const cookie = cookies[i].trim(); | ||
| if (cookie.substring(0, name.length + 1) === (name + '=')) { | ||
| cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return cookieValue; | ||
| } | ||
| }); | ||
| </script> | ||
| {% endblock %} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 '我的收藏' %} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[nitpick] Consider wrapping '收藏文章' in a translation function to ensure consistent internationalization support as seen elsewhere in the application.