index.html 9.2 KB

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