Browse Source

Initial commit

main
sipp11 2 years ago
commit
a58b7ec818
  1. 5
      .gitignore
  2. 0
      Djangoproject/__init__.py
  3. 16
      Djangoproject/asgi.py
  4. 136
      Djangoproject/settings.py
  5. 24
      Djangoproject/urls.py
  6. 16
      Djangoproject/wsgi.py
  7. 0
      Mywebsite/__init__.py
  8. 4
      Mywebsite/admin.py
  9. 5
      Mywebsite/apps.py
  10. 24
      Mywebsite/migrations/0001_initial.py
  11. 19
      Mywebsite/migrations/0002_auto_20200902_0053.py
  12. 18
      Mywebsite/migrations/0003_auto_20200902_1445.py
  13. 19
      Mywebsite/migrations/0004_auto_20200902_1534.py
  14. 0
      Mywebsite/migrations/__init__.py
  15. 15
      Mywebsite/models.py
  16. 13
      Mywebsite/static/css/mycss.css
  17. BIN
      Mywebsite/static/images/brand.jpg
  18. BIN
      Mywebsite/static/images/logo.jpg
  19. BIN
      Mywebsite/static/images/stop.png
  20. 9
      Mywebsite/static/js/js1.js
  21. 13
      Mywebsite/templates/Mywebsite/404error.html
  22. 27
      Mywebsite/templates/Mywebsite/addnews.html
  23. 24
      Mywebsite/templates/Mywebsite/contentedit.html
  24. 94
      Mywebsite/templates/Mywebsite/contentnewsmanager.html
  25. 99
      Mywebsite/templates/Mywebsite/header.html
  26. 30
      Mywebsite/templates/Mywebsite/index.html
  27. 23
      Mywebsite/templates/Mywebsite/login.html
  28. 13
      Mywebsite/templates/Mywebsite/postfrom.html
  29. 47
      Mywebsite/templates/Mywebsite/regisuser.html
  30. 19
      Mywebsite/templates/Mywebsite/result.html
  31. 12
      Mywebsite/templates/Mywebsite/warning.html
  32. 3
      Mywebsite/tests.py
  33. 24
      Mywebsite/urls.py
  34. 131
      Mywebsite/views.py
  35. 10
      manage.py
  36. 4
      requirements.txt

5
.gitignore vendored

@ -0,0 +1,5 @@
.DS_Store
__pycache__
*.pyc
.vscode
*.sqlite3

0
Djangoproject/__init__.py

16
Djangoproject/asgi.py

@ -0,0 +1,16 @@
"""
ASGI config for Djangoproject project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Djangoproject.settings')
application = get_asgi_application()

136
Djangoproject/settings.py

@ -0,0 +1,136 @@
"""
Django settings for Djangoproject project.
Generated by 'django-admin startproject' using Django 3.1.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '9t4uov^!)o-08xxy^)(h9n7_a_tl_=j81ugwv&wl-)%n$pf3hs'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Mywebsite',
'ckeditor',
'django_wysiwyg',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Djangoproject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'Mywebsite', 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'Djangoproject.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'NAME': 'db.sqlite3',
'ENGINE': 'django.db.backends.sqlite3',
'USER': '',
'PASSWORD': ''
},
# 'default': {
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME':'mydjango',
# 'USER':'root',
# 'HOST':'localhost',
# 'PORT':''
# },
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'th-TH'
TIME_ZONE = 'Asia/Bangkok'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'
DJANGO_WYSIWYG_FLAVOR = "ckeditor"

24
Djangoproject/urls.py

@ -0,0 +1,24 @@
"""Djangoproject URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('Mywebsite.urls')),
]
handler404 = 'Mywebsite.views.handler404'

16
Djangoproject/wsgi.py

@ -0,0 +1,16 @@
"""
WSGI config for Djangoproject project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Djangoproject.settings')
application = get_wsgi_application()

0
Mywebsite/__init__.py

4
Mywebsite/admin.py

@ -0,0 +1,4 @@
from django.contrib import admin
from .models import tb_news
# Register your models here.
admin.site.register(tb_news)

5
Mywebsite/apps.py

@ -0,0 +1,5 @@
from django.apps import AppConfig
class MywebsiteConfig(AppConfig):
name = 'Mywebsite'

24
Mywebsite/migrations/0001_initial.py

@ -0,0 +1,24 @@
# Generated by Django 3.1 on 2020-08-30 09:36
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='tb_news',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('news_title', models.CharField(max_length=300)),
('news_detail', models.TextField()),
('news_photo', models.ImageField(default='', upload_to='photo')),
('news_date', models.DateTimeField(auto_now=True)),
],
),
]

19
Mywebsite/migrations/0002_auto_20200902_0053.py

@ -0,0 +1,19 @@
# Generated by Django 3.1 on 2020-09-01 17:53
import ckeditor.fields
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('Mywebsite', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='tb_news',
name='news_detail',
field=ckeditor.fields.RichTextField(blank=True, null=True),
),
]

18
Mywebsite/migrations/0003_auto_20200902_1445.py

@ -0,0 +1,18 @@
# Generated by Django 3.1 on 2020-09-02 07:45
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('Mywebsite', '0002_auto_20200902_0053'),
]
operations = [
migrations.AlterField(
model_name='tb_news',
name='news_detail',
field=models.TextField(default=''),
),
]

19
Mywebsite/migrations/0004_auto_20200902_1534.py

@ -0,0 +1,19 @@
# Generated by Django 3.1 on 2020-09-02 08:34
import ckeditor.fields
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('Mywebsite', '0003_auto_20200902_1445'),
]
operations = [
migrations.AlterField(
model_name='tb_news',
name='news_detail',
field=ckeditor.fields.RichTextField(blank=True, null=True),
),
]

0
Mywebsite/migrations/__init__.py

15
Mywebsite/models.py

@ -0,0 +1,15 @@
from django.db import models
from django.core.files.storage import FileSystemStorage
import os
from ckeditor.fields import RichTextField
# Create your models here.
class tb_news(models.Model):
news_title = models.CharField(max_length=300)
#news_detail = models.TextField(default='')
news_detail = RichTextField(blank=True,null=True)
news_photo = models.ImageField(upload_to='photo',default='')
news_date = models.DateTimeField(auto_now=True,blank=False)

13
Mywebsite/static/css/mycss.css

@ -0,0 +1,13 @@
body{
background-color: rgb(251, 251, 251);
}
img {
max-width: 100%;
height: auto !important;
display: block;
margin-left: auto;
margin-right: auto;
}

BIN
Mywebsite/static/images/brand.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 403 KiB

BIN
Mywebsite/static/images/logo.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

BIN
Mywebsite/static/images/stop.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

9
Mywebsite/static/js/js1.js

@ -0,0 +1,9 @@
function js1(){
alert("คณยงไมไดบอนญาตในการเพมขอมลขาว กรณารอ admin ตรวจสอบขอมล")
}
function js2(){
alert("ไมอนญาตใหลบขอมลครบ")
}

13
Mywebsite/templates/Mywebsite/404error.html

@ -0,0 +1,13 @@
{% extends 'Mywebsite/header.html' %}
{% block content %}
{% load static %}
<script src="{% static 'js/js1.js' %}"></script>
<link rel="stylesheet" href="{% static 'css/mycss.css'%}">
<br>
<img src="{% static 'images/stop.png' %}" class="center">
<h1>404 ERROR</h1>
<a href="/" class="btn btn-success">กลบไปยงหนาหล</a>
{% endblock %}

27
Mywebsite/templates/Mywebsite/addnews.html

@ -0,0 +1,27 @@
{% extends 'Mywebsite/header.html' %}
{% block content %}
{% load wysiwyg %}
{% wysiwyg_setup %}
<br>
<h1>เพมขอมลขาวสาร</h1>
<form action="{% url 'addnewsdata' %}" enctype="multipart/form-data" method="POST">
{% csrf_token %}
<div class='form-group'>
<label for="">อขาวสาร</label>
<input type="text" class="form-control" name="news_title" required>
</div>
<div class='form-group'>
<label for="">รายละเอยดขาวสาร</label>
<textarea id="detail" type="text" class="form-control" name="news_detail" rows="5" required></textarea>
</div>
<div class="form-group">
<input type='file' name='news_photo' accept="image/x-png,image/gif,image/jpeg" required>
<img id="blah" src="#" alt="" class="img-thumbnail" />
</div>
<input type="submit" value="บนทกขาวสาร" class="btn btn-success">
</form>
{% wysiwyg_editor "detail" %}
{% endblock %}

24
Mywebsite/templates/Mywebsite/contentedit.html

@ -0,0 +1,24 @@
{% extends 'Mywebsite/header.html' %}
{% block content %}
<br>
<h1>แกไขขอม</h1>
{% for data in result %}
<form action="{% url 'contentupdate' %}" enctype="multipart/form-data" method="POST">
{% csrf_token %}
<input type="hidden" value="{{data.id}}" name="id">
<div class='form-group'>
<label for="">อขาวสาร</label>
<input type="text" class="form-control" name="news_title" value="{{data.news_title}}">
</div>
<div class='form-group'>
<label for="">รายละเอยดขาวสาร</label>
<textarea type="text" class="form-control" name="news_detail" rows="5">{{data.news_detail}}</textarea>
</div>
<div class="card mb-3">
<img class="card-img-top" src="{{ data.news_photo.url}}" alt="Card image cap">
</div>
<input type="submit" value="แกไขขาวสาร" class="btn btn-success">
</form>
{%endfor%}
{% endblock %}

94
Mywebsite/templates/Mywebsite/contentnewsmanager.html

@ -0,0 +1,94 @@
{% extends 'Mywebsite/header.html' %}
{% block content %}
{% load static %}
<script src="{% static 'js/js1.js' %}"></script>
<br>
<h1>อมลขาวสาร</h1>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">วข</th>
<th scope="col">นท</th>
<th scope="col">แกไข</th>
<th scope="col">ลบ</th>
</tr>
</thead>
<tbody>
{% for data in news %}
<tr>
<th scope="row">{{data.id}}</th>
<td>{{data.news_title}}</td>
<td>{{data.news_date}}</td>
{% if user.first_name == 'worrachag' %}
<td>
<form action="{% url 'contentedit' %}" method="get">
<input type="hidden" value="{{data.id}}" name="id">
<button type="submit" class="btn btn-primary">แกไข</button>
</form>
</td>
<td><button type="submit" class="btn btn-danger" data-toggle="modal"
data-target="#exampleModal{{data.id}}">ลบขอม</button></td>
{% else %}
<td>
<button onclick="myFunction1()" type="button" class="btn btn-primary">แกไข</button>
</td>
<td>
<button onclick="myFunction()" type="button" class="btn btn-danger">ลบขอม</button>
</td>
{% endif %}
</tr>
<!-- Modal -->
<div class="modal fade" id="exampleModal{{data.id}}" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<form action="{% url 'contentdelete' %} " method="POST">
{% csrf_token %}
<input type="hidden" value="{{data.id}}" name="id">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">นยนการลบขอม</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
ณตองการลบขอมล Title {{data.news_title}} หรอไม
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">นยนการลบขอม</button>
</div>
</div>
</form>
</div>
</div>
{% endfor %}
</tbody>
</table>
<script>
function myFunction() {
alert("ไมไดบสทธใหลบขอมล");
}
function myFunction1() {
alert("ไมไดบสทธใหแกไขขอมล");
}
</script>
{% endblock %}

99
Mywebsite/templates/Mywebsite/header.html

@ -0,0 +1,99 @@
<!DOCTYPE html>
{% load static %}
<script src="{% static 'js/js1.js' %}"></script>
<html lang="en">
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Django Web</title>
<link rel="stylesheet" href="{% static 'css/mycss.css'%}">
<script src="https://cdn.ckeditor.com/4.14.1/standard/ckeditor.js"></script>
</head>
<body >
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="/"> เขยนโคดยามวาง</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="/regisuser">ลงทะเบยนผใชงาน</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
ดการขาวสาร
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="/addnews">เพมขาวสาร</a>
<a class="dropdown-item" href="/contentmanager">ดการขาวสาร</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="/logoff">Logoff</a>
</li>
<li class="nav-item">
<a class="nav-link">สวสดครบคณ {{ user.first_name }} {{ user.last_name }}</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="/login">Login เขาระบบ</a>
</li>
{% endif %}
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<div class="container">
{% block content %}
{% endblock %}
</div>
<!-- Footer -->
<footer class="page-footer font-small blue">
<!-- Copyright -->
<div class="footer-copyright text-center py-3">© 2020 Copyright:
<a href="https://mdbootstrap.com/">เขยนโคดยามวาง</a>
</div>
<!-- Copyright -->
</footer>
<!-- Footer -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"
integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
crossorigin="anonymous"></script>
</body>
</html>

30
Mywebsite/templates/Mywebsite/index.html

@ -0,0 +1,30 @@
{% extends 'Mywebsite/header.html' %}
{% block content %}
{% load static %}
<br>
<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="{% static 'images/brand.jpg'%} " alt="First slide">
</div>
</div>
</div>
<hr>
<br>
{% for data in news %}
<form action="{% url 'contentresult' %}" method="get">
<div class="shadow p-3 mb-5 bg-white rounded">
<input type="hidden" name="id" value="{{data.id}}">
<img class="card-img-top" src="{{ data.news_photo.url}}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">{{data.news_title}}</h5>
<p class="card-text"><small class="text-muted">{{data.news_date}}</small></p>
<input type="submit" value="อานตอ" class="btn btn-success">
</div>
</div>
</form>
{%endfor%}
{% endblock %}

23
Mywebsite/templates/Mywebsite/login.html

@ -0,0 +1,23 @@
{% extends 'Mywebsite/header.html' %}
{% block content %}
<br>
<h1>Login เขาใชงานระบบ</h1>
{% for msg in messages %}
<div class="alert alert-danger" role="alert">
{{msg}}
</div>
{% endfor %}
<form action="{% url 'logincheck' %}" method="POST">
{% csrf_token %}
<div class='form-group'>
<label for="">Username</label>
<input type="text" class="form-control" name="username" required>
</div>
<div class='form-group'>
<label for="">Password</label>
<input type="password" class="form-control" name="password" required>
</div>
<input type="submit" value="Login เขาใชงาน" class="btn btn-success">
</form>
{% endblock %}

13
Mywebsite/templates/Mywebsite/postfrom.html

@ -0,0 +1,13 @@
{% extends 'Mywebsite/header.html' %}
{% block content %}
<br>
<h1>เพมขอมลขาวสาร</h1>
<form action="{% url 'addnewsdata' %}" enctype="multipart/form-data" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="บนทกขาวสาร" class="btn btn-success">
</form>
{% endblock %}

47
Mywebsite/templates/Mywebsite/regisuser.html

@ -0,0 +1,47 @@
{% extends 'Mywebsite/header.html' %}
{% block content %}
<br>
<h1>ลงทะเบยนผใชงาน</h1>
{% for msg in messages %}
<div class="alert alert-danger" role="alert">
{{msg}}
</div>
{% endfor %}
<form action="{% url 'regisuserdata' %}" method="POST">
{% csrf_token %}
<div class='form-group'>
<label for=""></label>
<input type="text" class="form-control" name="fname" required>
</div>
<div class='form-group'>
<label for="">นามสก</label>
<input type="text" class="form-control" name="lname" required>
</div>
<div class='form-group'>
<label for="">Email</label>
<input type="email" class="form-control" name="email" required>
</div>
<div class='form-group'>
<label for="">Username</label>
<input type="text" class="form-control" name="username" required>
</div>
<div class='form-group'>
<label for="">Password</label>
<input type="password" class="form-control" name="password" required>
</div>
<div class='form-group'>
<label for="">RE-Password</label>
<input type="password" class="form-control" name="repassword" required>
</div>
<input type="submit" value="บนทกการลงทะเบยน" class="btn btn-success">
</form>
{% endblock %}

19
Mywebsite/templates/Mywebsite/result.html

@ -0,0 +1,19 @@
{% extends 'Mywebsite/header.html' %}
{% block content %}
<br>
{% for data in result %}
<input type="hidden" value="{{data.id}}" name="id">
<div class='form-group'>
<h1>{{data.news_title}}</h1>
</div>
<div class="shadow p-3 mb-5 bg-white rounded">
<div class="card-body">
<p>{{data.news_detail | safe }}</p>
</div>
</div>
{%endfor%}
{% endblock %}

12
Mywebsite/templates/Mywebsite/warning.html

@ -0,0 +1,12 @@
{% extends 'Mywebsite/header.html' %}
{% block content %}
{% load static %}
<script src="{% static 'js/js1.js' %}"></script>
<script>js1()</script>
<link rel="stylesheet" href="{% static 'css/mycss.css'%}">
<br>
<img src="{% static 'images/stop.png' %}" class="center">
<h1>ณยงไมไดบอนญาตในการเพมขอมลขาว กรณารอ admin ตรวจสอบขอม</h1>
{% endblock %}

3
Mywebsite/tests.py

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

24
Mywebsite/urls.py

@ -0,0 +1,24 @@
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('',views.index),
path('addnews/',views.addnews),
path('addnewsdata/',views.addnewsdata,name='addnewsdata'),
path('contentmanager/',views.contentmanager),
path('contentedit/',views.contentedit,name='contentedit'),
path('contentupdate/',views.contentupdate,name='contentupdate'),
path('contentdelete/',views.contentdelete,name='contentdelete'),
path('contentresult/',views.contentresult,name='contentresult'),
path('regisuser/',views.regisuser),
path('regisuserdata/',views.regisuserdata,name='regisuserdata'),
path('login/',views.login),
path('logincheck/',views.logincheck,name='logincheck'),
path('logoff/',views.logoff),
path('warning/',views.warning)
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)

131
Mywebsite/views.py

@ -0,0 +1,131 @@
from django.shortcuts import render,redirect
from django.http import HttpResponse
from .models import tb_news
from django.contrib.auth.models import User,auth
from django.contrib import messages
from django.contrib.auth.decorators import login_required,permission_required
# Create your views here.
def handler404(request, exception):
return render(request,'Mywebsite/404error.html')
def index(request):
content = tb_news.objects.all().order_by("-id")
return render(request,'Mywebsite/index.html',{'news':content})
@login_required(login_url='/login')
@permission_required('is_staff',login_url='/warning')
def addnews(request):
return render(request,'Mywebsite/addnews.html')
#news_title news_detail news_photo news_date
def addnewsdata(request):
news_title = request.POST['news_title']
news_detail = request.POST['news_detail']
news_photo = request.FILES['news_photo']
content = tb_news(news_title=news_title,news_detail=news_detail,news_photo=news_photo)
content.save()
return redirect("/contentmanager")
@login_required(login_url='/login')
@permission_required('is_staff',login_url='/warning')
def contentmanager(request):
mydatanews = tb_news.objects.all()
return render(request,'Mywebsite/contentnewsmanager.html',{'news':mydatanews})
def contentedit(request):
id = request.GET['id']
result = tb_news.objects.filter(pk=id)
print(result)
return render(request,'Mywebsite/contentedit.html',{'result':result})
def contentupdate(request):
id = request.POST['id']
news_title = request.POST['news_title']
news_detail = request.POST['news_detail']
try:
news_photo = request.FILES['news_photo']
except KeyError:
news_photo = None
content = tb_news.objects.get(pk=id)
content.news_title = news_title
content.news_detail = news_detail
if news_photo is not None:
content.news_photo = news_photo
content.save()
return redirect("/contentmanager")
def contentdelete(request):
id = request.POST['id']
content = tb_news.objects.get(pk=id)
content.delete()
return redirect("/contentmanager")
def contentresult(request):
id = request.GET['id']
content = tb_news.objects.filter(pk=id)
return render(request,'Mywebsite/result.html',{'result':content})
def regisuser(request):
return render(request,'Mywebsite/regisuser.html')
def regisuserdata(request):
fname = request.POST['fname']
lname = request.POST['lname']
email = request.POST['email']
username = request.POST['username']
password = request.POST['password']
repassword = request.POST['repassword']
if password == repassword:
if User.objects.filter(username=username).exists():
messages.error(request,"Username ซำในระบบ")
return redirect("/regisuser")
elif User.objects.filter(email=email).exists():
messages.error(request,"Email ซำในระบบ")
return redirect("/regisuser")
else:
user = User.objects.create_user(
first_name=fname,
last_name=lname,
username=username,
password=password,
email=email
)
user.save()
return redirect("/")
else:
messages.error(request,"Password และ Repassword ไมตรงก")
return redirect("/regisuser")
def login(request):
if request.user.is_authenticated:
return redirect("/")
return render(request,'Mywebsite/login.html')
def logincheck(request):
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username,password=password)
if user is not None:
auth.login(request,user)
return redirect('/')
else:
messages.error(request,"ไมพบผใชงานในระบบ")
return redirect('/login')
def logoff(request):
auth.logout(request)
return redirect("/login")
def warning(request):
return render(request,'Mywebsite/warning.html')

10
manage.py

@ -0,0 +1,10 @@
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Djangoproject.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)

4
requirements.txt

@ -0,0 +1,4 @@
django
django_wysiwyg
django-ckeditor
Pillow
Loading…
Cancel
Save