index.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. {% extends "_common/base.html" %}
  2. {% block css %}
  3. {% load avatar_tags %}
  4. {% load day_util %}
  5. {% endblock %}
  6. {% block container %}
  7. <div class="ui grid stackable container">
  8. <div class="row" id="article">
  9. <div class="eleven wide column">
  10. <div class="ui feed">
  11. <h2>动态</h2>
  12. <div class="ui divider"></div>
  13. {% for item in articles_new %}
  14. {% if item.status == 1 %}
  15. {% if item.type == 1 %}
  16. <div class="event">
  17. <div class="label">
  18. <img src="/user_avatar/{{ item.user_id }}">
  19. </div>
  20. <div class="content">
  21. <div class="date"
  22. style="font-size: 1.1em">{{ item.created_time | days_until }}</div>
  23. <div class="summary"><a>{{ item.user__first_name }}{{ item.user__last_name }}</a>
  24. 发布了{{ item.category__name }}:&nbsp;<a
  25. href="/article/{{ item.id }}.html">{{ item.title }}</a></div>
  26. <div class="extra text">{{ item.intro }}
  27. &nbsp;&nbsp;&nbsp;Tags:
  28. {% for tag in item.tags %}
  29. <a href="/tag/{{ tag }}?page=1"><span
  30. class="label label-success">{{ tag }}</span></a>
  31. {% endfor %}</div>
  32. </div>
  33. </div>
  34. {% elif item.type == 2 %}
  35. <div class="event">
  36. <div class="label">
  37. <img src="/user_avatar/{{ item.user_id }}">
  38. </div>
  39. <div class="content">
  40. <div class="date"
  41. style="font-size: 1.1em">{{ item.created_time | days_until }}</div>
  42. <div class="summary"><a>{{ item.user__first_name }}{{ item.user__last_name }}</a>
  43. 发布了{{ item.category__name }}:&nbsp;<a
  44. href="/article/{{ item.id }}.html">{{ item.title }}</a></div>
  45. {% autoescape off %}
  46. <div class="extra images">
  47. {{ item.html_text }}
  48. </div>
  49. {% endautoescape %}
  50. </div>
  51. </div>
  52. {% endif %}
  53. {% endif %}
  54. {% endfor %}
  55. {% if is_login %}
  56. <h3>发布动态</h3>
  57. <div class="ui divider"></div>
  58. <form class="ui reply form">
  59. {% csrf_token %}
  60. <div class="field">
  61. <!-- 编辑器 DOM -->
  62. <input placeholder="文字" name="title" class="ui input">
  63. <div style="border: 1px solid #ccc;margin-top: 5px">
  64. <div id="editor-toolbar" style="border-bottom: 1px solid #ccc;"></div>
  65. <div id="editor-text-area"></div>
  66. </div>
  67. </div>
  68. <div onclick="submit()" style="float: right" class="ui primary submit labeled icon button">
  69. <i class="icon send"></i> 发布
  70. </div>
  71. </form>
  72. {% endif %}
  73. </div>
  74. </div>
  75. <div class="four wide right floated column">
  76. <h4 class="ui header">归档</h4>
  77. <div class="ui list">
  78. {% for record in records %}
  79. <a class="item" href="/date/{{ record.datetime }}?page=1">{{ record.datetime }}
  80. ({{ record.count }})</a>
  81. {% endfor %}
  82. </div>
  83. <h4 class="ui header">Top</h4>
  84. <div class="ui list">
  85. {% for tag in tags %}
  86. <a class="item" href="/tag/{{ tag.name }}?page=1">{{ tag.name }} ({{ tag.count }})</a>
  87. {% endfor %}
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. {% endblock %}
  93. {% block js %}
  94. {% if is_login %}
  95. <script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
  96. <script>
  97. const E = window.wangEditor
  98. // 切换语言
  99. const LANG = location.href.indexOf('lang=en') > 0 ? 'en' : 'zh-CN'
  100. E.i18nChangeLanguage(LANG)
  101. const imageToHtmlConf = {
  102. type: 'image',
  103. elemToHtml: function imageToHtml(elemNode) {
  104. const {src, alt, href = '', style = {}, width, height} = elemNode || {}
  105. return `<a data-pswp-width="${alt.split(',')[1]}" data-pswp-height="${alt.split(',')[1]}" href="${src}"><img data-magnify="gallery" data-src="${src}" src="${src}" data-href="${href}" "/></a>`
  106. }
  107. };
  108. const editorConfig = {
  109. placeholder: 'Type here...',
  110. scroll: false, // 禁止编辑器滚动
  111. MENU_CONF: {
  112. uploadImage: {
  113. server: '/management/files/upload',
  114. // form-data fieldName ,默认值 'wangeditor-uploaded-image'
  115. fieldName: 'file',
  116. // 单个文件的最大体积限制,默认为 2M
  117. maxFileSize: 4 * 1024 * 1024, // 1M
  118. // 最多可上传几个文件,默认为 100
  119. maxNumberOfFiles: 10,
  120. // 选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []
  121. allowedFileTypes: ['image/*'],
  122. // 自定义上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。
  123. meta: {
  124. csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val()
  125. },
  126. // 将 meta 拼接到 url 参数中,默认 false
  127. metaWithUrl: false,
  128. // 自定义增加 http header
  129. // 跨域是否传递 cookie ,默认为 false
  130. withCredentials: true,
  131. // 超时时间,默认为 10 秒
  132. timeout: 5 * 1000, // 5 秒
  133. }
  134. },
  135. onChange(editor) {
  136. const html = editor.getHtml()
  137. $('#title').val($('#page_title').val())
  138. }
  139. }
  140. E.Boot.registerElemToHtml(imageToHtmlConf);
  141. // 先创建 editor
  142. const editor = E.createEditor({
  143. selector: '#editor-text-area',
  144. content: [],
  145. // html: '',
  146. config: editorConfig
  147. })
  148. // 创建 toolbar
  149. const toolbar = E.createToolbar({
  150. editor,
  151. mode: 'simple',
  152. selector: '#editor-toolbar',
  153. config: {
  154. excludeKeys: ['blockquote', 'header1', 'header2', 'header3', '|', 'bold', 'underline', 'italic', 'through', 'color', 'bgColor', 'clearStyle', '|', 'bulletedList', 'numberedList', 'todo', 'justifyLeft', 'justifyRight', 'justifyCenter', '|', 'insertLink', 'insertVideo', 'insertTable', 'codeBlock', '|', 'undo', 'redo', '|', 'fullScreen']
  155. }
  156. })
  157. </script>
  158. {% endif %}
  159. <script>
  160. function submit() {
  161. let formData = new FormData()
  162. formData.append('title', $("input[name='title']").val())
  163. formData.append('intro', editor.getText())
  164. formData.append('category', 6)
  165. formData.append('html', editor.getHtml())
  166. formData.append('markdown', editor.getText())
  167. formData.append('type', 2)
  168. formData.append('tags', '')
  169. formData.append('status', 1)
  170. formData.append('is_top', 1)
  171. formData.append('csrfmiddlewaretoken', $("input[name='csrfmiddlewaretoken']").val())
  172. $.ajax('/management/article/add_article', {
  173. method: 'post',
  174. data: formData,
  175. contentType: false,
  176. processData: false,
  177. success: function (res) {
  178. window.location.href = '/'
  179. },
  180. error: function (err) {
  181. }
  182. })
  183. }
  184. </script>
  185. {% endblock %}
  186. {% load static %}