我无法弄清楚如何控制模型表单的字段验证。
这是我的表格模型:
class UserForm(ModelForm):
class Meta:
model = User
fields = ('email', 'password')
Run Code Online (Sandbox Code Playgroud)
以下是我在模板中呈现表单的方式:
<form method="post" action="">
{% csrf_token %}
{{ userform.as_p }}
<input type="submit" value="Submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
这是我的观点:
def login_page(request):
if request.method == 'POST':
userform = UserForm(request.POST)
if userform.is_valid():
email = request.POST['email']
password = request.POST['password']
user = authenticate(username=email, password=password)
if user is not None:
login(request, user)
return redirect('/dashboard/')
else:
# How can I give the user feedback about authentication failute here. Right now it just reloads the form without …Run Code Online (Sandbox Code Playgroud) 我有一个计算人口增长的计划.它似乎有效,但是当人口超过100万时,输出的十进制数增加到10的幂.(这被称为科学符号吗?指数形式?我忘了.)
无论如何输出数据作为完整数字?这是输出的代码,我将不得不转换它.
#include "header.h"
void output (float currentPopulation, float years, float birthRate, float deathRate)
{
cout << "the populaion in " << years << " years will be: " << estimatedPopulation (currentPopulation, years, birthRate, deathRate) << endl;
}
Run Code Online (Sandbox Code Playgroud)
新代码:
#include "header.h"
void output (float currentPopulation, float years, float birthRate, float deathRate)
{
cout << "the populaion in " << years << " years will be: " << fixed << setprecision(0) << estimatedPopulation (currentPopulation, years, birthRate, deathRate) << endl;
}
Run Code Online (Sandbox Code Playgroud) <button>在设计UI而不是使用时使用html 标签是不正确的<div class="button">?在实现中,我没有注意到两种方法之间的区别,并且<button>看起来更加语义,但我不确定它是否仅用于表单内的使用.该按钮不在表单中.使用<div>标签似乎是制作按钮(引导程序)最常用的方法,但我不确定为什么.这有什么技术原因吗?
据我所知,只有班级可以访问数据,因此它"更安全",但不是,但我真的不明白为什么它是如此重要.也许是因为我没有让任何程序复杂到数据可能会被意外改变但是在学习课程时被告知私有化是很重要因为当我改变的时候它更"安全"程序中的数据是我明确指出的.任何人都可以提供一些例子,如果这些数据不是私有的,那么数据会被无意改变吗?
给定以下模型:
class Post(models.Model):
title = models.CharField(max_length=200)
html = models.TextField()
class PostTag(models.Model):
post = models.ForeignKey('Post')
tag = models.CharField(max_length=200)
Run Code Online (Sandbox Code Playgroud)
我想完成基于给定PostTag的帖子查找。因此,如果我有两个帖子A和B标记为“ foo”,则我希望能够查找带有该标签的所有帖子,并取回帖子A和B。
我认为查询看起来像以下内容:
posts = Post.objects.filter(tag=tag)
Run Code Online (Sandbox Code Playgroud)
关于从何处开始实现此目标的任何提示?
我的程序启动并运行但是我的if语句出现了问题,除了正确的输出之外,还有"scalene"是输出(除非scalene是正确的输出).看一下这个问题:

谁能发现这个bug?
triangleShape函数
# include "header.h"
triangleType triangleShape(float sideLength1, float sideLength2, float sideLength3)
{
triangleType triangle;
if (sideLength1 + sideLength2 < sideLength3)
triangle = noTriangle;
else if (sideLength1 + sideLength3 < sideLength2)
triangle = noTriangle;
else if (sideLength3 + sideLength2 < sideLength1)
triangle = noTriangle;
else if (sideLength1 == sideLength2 == sideLength3)
triangle = equilateral;
else if (sideLength1 == sideLength2)
triangle = isoceles;
else if (sideLength1 == sideLength3)
triangle = isoceles;
else if (sideLength2 == sideLength3)
triangle = isoceles;
else …Run Code Online (Sandbox Code Playgroud) 我刚刚使用数组完成了我的第一个任务,我觉得它比它必须要复杂一点.该程序读取与分数它一个文件,并计数在一定范围内的分数的发生,然后输出出现的次数.
我想知道是否有更有效的方法来完成此任务(仅使用数组).我理解数组使我不必制作8个单独的变量,但仍有很多if语句!
头
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <cstdlib>
using namespace std;
void extern input(ifstream&, ofstream&, int&, int*);
void extern calculate (int, int*);
void extern output (ofstream&, int*);
#endif // HEADER_H_INCLUDED
Run Code Online (Sandbox Code Playgroud)
主要
#include "header.h"
int main()
{
int grade;
int array[8] = {0};
ifstream inData;
ofstream outData;
inData.open("Ch9_Ex4Data.txt");
if (!inData)
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
outData.open("DataOut.txt");
while (inData)
{
input(inData, outData, grade, array);
} …Run Code Online (Sandbox Code Playgroud) 我制作了一个包含多个y轴值的图表,这些值被绘制为圆圈.我现在想要用线连接每个y轴值(基本上是折线图).
我认为我的错误在于我如何尝试定义y轴线值:
var line = d3.svg.line()
.x(function(d) { return x(d.x); })
.y(function(d) { return y(d.y1, d.y2, d.y3); });
Run Code Online (Sandbox Code Playgroud)
这是我的数据集:
var dataset = [
{x: d3.time.hour.utc.offset(now, -5), y1: 1, y2: 3, y3: 2},
{x: d3.time.hour.utc.offset(now, -4), y1: 2, y2: 2, y3: 4},
{x: d3.time.hour.utc.offset(now, -3), y1: 3, y2: 3, y3: 1},
{x: d3.time.hour.utc.offset(now, -2), y1: 4, y2: 1, y3: 2},
{x: d3.time.hour.utc.offset(now, -1), y1: 5, y2: 5, y3: 3},
{x: now, y1: 6, y2: 4, y3: 3},
];
Run Code Online (Sandbox Code Playgroud)
以下是我的图表的完整示例:http: …
我正在编写一个程序,可以帮助我学习C++中的枚举数据类型.当前的麻烦是编译器在尝试使用新数据类型时不喜欢我的枚举用法,就像其他数据类型一样.在编译我的trangleShape函数时,我收到错误"重新声明为不同类型的符号".看看相关代码.任何见解都表示赞赏!谢谢!
(所有函数都是自己的.cpp文件.)
头文件
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <iostream>
#include <iomanip>
using namespace std;
enum triangleType {noTriangle, scalene, isoceles, equilateral};
//prototypes
void extern input(float&, float&, float&);
triangleType extern triangleShape(float, float, float);
/*void extern output (float, float, float);*/
void extern myLabel(const char *, const char *);
#endif // HEADER_H_INCLUDED
Run Code Online (Sandbox Code Playgroud)
主功能
//8.1 main
// this progam...
#include "header.h"
int main()
{
float sideLength1, sideLength2, sideLength3;
char response;
do //main loop
{
input (sideLength1, sideLength2, sideLength3);
triangleShape (sideLength1, sideLength2, sideLength3);
//output (sideLength1, sideLength2, …Run Code Online (Sandbox Code Playgroud) 我收到错误:"未定义引用'yClass :: yClass()'
尝试在main中创建类的实例时.谁知道为什么?
标题:
#ifndef header_h
#define header_h
#include <cstdlib>
#include <iostream>
using namespace std;
class yClass
{
public:
void one();
void two(int,int);
yClass();
private:
int a;
int b;
};
#endif
Run Code Online (Sandbox Code Playgroud)
主要:
#include "header.h"
int main()
{
yClass a;
system("PAUSE");
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud) 我正在进行一项任务,向我介绍运算符重载.我必须将一些二元运算符作为成员函数和朋友函数重载.我的成员函数重载"+"运算符工作正常但我的朋友函数重载" - "运算符似乎很难找到成员函数能够使用的数据.
class def:
class matrix
{
friend ostream& operator << (ostream&, const matrix&);
friend bool operator == (const matrix &, const matrix &);
friend matrix operator - (const matrix &, const matrix &);
private:
int size;
int range;
int array[10][10];
public:
matrix(int);
matrix(int, int);
bool operator != (const matrix &) const;
matrix operator + (const matrix &) const;
const matrix & operator = (const matrix &);
};
Run Code Online (Sandbox Code Playgroud)
"+"过载:
matrix matrix::operator + (const matrix & a) const
{
matrix …Run Code Online (Sandbox Code Playgroud)