我无法上传文件。 按保存。然后消息错误“错误请求(#400)缺少必需参数:id”。
我曾尝试做过$ model->保存(假);但是当我这样做时,只上传文字,而不上传图片。
文件后端\ views \ album \ _form.php
<?= $form->field($model, 'file')->widget(FileInput::classname(), [
'options' => ['accept' => 'image/*'],
'pluginOptions' => [
'initialPreview'=>[],
'allowedFileExtensions'=>['jpg', 'png','gif'],
'showPreview' => true,
'showRemove' => true,
'showUpload' => false
]
]); ?>
文件后端\ models \ Album.php
public static function tableName()
{
return 'album';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['album_name', 'album_detail', 'album_cover', 'album_create', 'album_status'], 'required'],
[['album_create'], 'safe'],
[['album_status'], 'string'],
[['file'],'file'],
[['album_name', 'album_cover'], 'string', 'max' => 100],
[['album_detail'], 'string', 'max' => 255],
];
}
文件后端\ controllers \ AlbumController.php
public function actionCreate()
{
$model = new Album();
if ($model->load(Yii::$app->request->post()))
{
// get the instance of the uploaded file
$imageName = $model->album_name;
$model->file = UploadedFile::getInstance($model,'file');
$model->file->saveAs( 'uploads/'.$imageName.'.'.$model->file->extension );
// save the path in the db column
$model->album_cover = 'uploads/'.$imageName.'.'.$model->file->extension;
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
答案 0 :(得分:0)
在import pygame
import random
import time
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
segment_width = 16
segment_height = 16
segment_margin = 4
x_change = segment_width + segment_margin
y_change = 0
class Segment(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([segment_width, segment_height])
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def draw(self, screen):
screen_rect = screen.get_rect()
screen.blit(self.image, self.rect)
if self.rect.left < screen_rect.left:
temp_rect = self.rect.copy()
temp_rect.x += screen_rect.width
screen.blit(self.image, temp_rect)
elif self.rect.right > screen_rect.right:
temp_rect = self.rect.copy()
temp_rect.x -= screen_rect.width
screen.blit(self.image, temp_rect)
if self.rect.top < screen_rect.top:
temp_rect = self.rect.copy()
temp_rect.y += screen_rect.height
screen.blit(self.image, temp_rect)
elif self.rect.bottom > screen_rect.bottom:
temp_rect = self.rect.copy()
temp_rect.y -= screen_rect.height
screen.blit(self.image, temp_rect)
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen_rect = screen.get_rect()
pygame.display.set_caption('Snakes')
allspriteslist = pygame.sprite.Group()
snake_segments = []
for i in range(3):
x = 400 - (segment_width + segment_margin) * 1
y = 200
segment = Segment(x, y)
snake_segments.append(segment)
allspriteslist.add(segment)
apple_segments = []
for i in range(1):
x = 200 - (segment_width + segment_margin) * 1
y = 100
segment = Segment(x, y)
apple_segments.append(segment)
allspriteslist.add(segment)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = (segment_width + segment_margin) * -1
y_change = 0
if event.key == pygame.K_RIGHT:
x_change = (segment_width + segment_margin)
y_change = 0
if event.key == pygame.K_UP:
x_change = 0
y_change = (segment_height + segment_margin) * -1
if event.key == pygame.K_DOWN:
x_change = 0
y_change = (segment_height + segment_margin)
old_segment = snake_segments.pop()
allspriteslist.remove(old_segment)
x = snake_segments[0].rect.x + x_change
y = snake_segments[0].rect.y + y_change
if x < screen_rect.left:
x += screen_rect.width
elif x + segment_width + segment_margin > screen_rect.right:
x -= screen_rect.width
if y < screen_rect.top:
y += screen_rect.height
elif y + segment_height + segment_margin > screen_rect.bottom:
y -= screen_rect.height
segment = Segment(x, y)
snake_segments.insert(0, segment)
allspriteslist.add(segment)
screen.fill(BLACK)
# allspriteslist.draw(screen)
for x in allspriteslist:
x.draw(screen)
pygame.display.flip()
clock.tick(5)
pygame.quit()
中,您没有检查actionCreate()
是否成功。添加:
save()
从以下位置删除if($model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
声明:
else
离开:
else {
return $this->render('create', [
'model' => $model,
]);
}
可能你的模型没有保存,正如你所说的那样运行它return $this->render('create', [
'model' => $model,
]);
保存它而不进行任何验证。尝试在$model->save(false)
或$model->save()
后以最简单的方式调试为什么不能保存:
$model->validate()