




老师的testcase/test_views.py
from django.test import TestCase
from django.test import Client
from jobs.models import Job, JobTypes, Cities
class JobTests(TestCase):
@classmethod
def setUpTestData(cls):
# Set up data for the whole TestCase
cls.job = Job.objects.create(job_name="Java开发工程师", job_type=JobTypes[0][0], job_city=Cities[1][0], job_requirement="精通Java开发")
def test1(self):
# Some test using self.job
pass
def test_index(self):
client = Client()
response = client.get('/joblist/')
self.assertEqual(response.status_code, 200)
def test_detail(self):
# 使用 TestCase.self.client 作为 HTTP Client:
response = self.client.get('/job/1/')
self.assertEqual(response.status_code, 200)
job = response.context['job']
self.assertEqual(job.job_name, JobTests.job.job_name)
我的testcase/test_views.py
from django.test import TestCase
from django.test import Client
from GAGA_meeting.models import project
class projectTests(TestCase):
@classmethod
def setUpTestData(cls):
# Set up data for the whole TestCase
cls.job = project.objects.create(project_name="MA", project_owner_name='season',)
def test1(self):
# Some test using self.job
pass
def test_index(self):
client = Client()
response = client.get('admin/')
self.assertEqual(response.status_code, 200)
def test_detail(self):
# 使用 TestCase.self.client 作为 HTTP Client:
response = self.client.get('')
self.assertEqual(response.status_code, 200)
job = response.context['job']
self.assertEqual(job.job_name, JobTests.job.job_name)






同步服务器:uWSGI与gunicorn功能类似,都需要与Nginx配合,选择一个即可。
异步服务器:Uvicorn(有异步views可用此服务器)



























https://github.com/korfuri/django-prometheus







https://django-simple-captcha.readthedocs.io/en/latest/usage.html


from django import forms
from django.contrib.auth import authenticate, login
from django.contrib.auth.forms import AuthenticationForm
from django.contrib import messages
from captcha.fields import CaptchaField # 导入模块
from django.shortcuts import render
from django.http.response import HttpResponseRedirect
from django.urls import reverse_lazy
from ratelimit.decorators import ratelimit
import logging
logger = logging.getLogger(__name__)
class CaptchaLoginForm(AuthenticationForm):
# username = forms.CharField(label='用户名')
# password = forms.CharField(widget=forms.PasswordInput, label='密码')
captcha = CaptchaField(label='验证码')
max_failed_login_count = 3
@ratelimit(key='ip', rate='5/m', block=True)
def login_with_captcha(request):
if request.POST:
failed_login_count = request.session.get('failed_login_count', 0)
# 没有连续的登陆失败, 使用默认的登陆页; 连续 n 次登陆失败, 要求输入验证码
if failed_login_count >= max_failed_login_count :
form = CaptchaLoginForm(data=request.POST)
else:
form = AuthenticationForm(data=request.POST)
# Validate the form: the captcha field will automatically
# check the input
if form.is_valid():
request.session['failed_login_count'] = 0
# authenticate user with credentials
user = authenticate(username=form.cleaned_data["username"], password=form.cleaned_data["password"])
if user is not None:
# attach the authenticated user to the current session
login(request,user)
return HttpResponseRedirect(reverse_lazy('admin:index'))
else:
failed_login_count += 1
request.session['failed_login_count'] = failed_login_count
logger.warning(" ----- failed login for user: %s, failed times:%s" % (form.data["username"], failed_login_count) )
if failed_login_count >= max_failed_login_count :
form = CaptchaLoginForm(request.POST)
messages.add_message(request, messages.INFO, 'Not a valid request')
else:
## 没有连续的登陆失败, 使用默认的登陆页; 连续 n 次登陆失败, 要求输入验证码
failed_login_count = request.session.get('failed_login_count', 0)
if failed_login_count >= max_failed_login_count :
form = CaptchaLoginForm(request.POST)
else:
form = AuthenticationForm()
return render(request, 'login.html', {'form': form})
INSTALLED_APPS = [
...
'captcha',
]


Rest Framework API限流

应用限流
https://django-ratelimit.readthedocs.io/en/stable/index.html


@ratelimit(key='ip', rate='5/m', block=True)
def login_with_captcha(request):





感觉就是HTTPS













# Password validation
# https://docs.djangoproject.com/en/3.2/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',
},
]
























我的实战https://blog.csdn.net/m0_46629123/article/details/126527984




就是安装Prometheus和Grafana

https://help.aliyun.com/document_detail/94622.html





https://help.aliyun.com/document_detail/87540.html



使用Jenkins来做CICD





https://kubernetes.io/zh-cn/docs/concepts/configuration/secret/




























