index.html 9.8 KB

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