当你写一个扩展Django的视图DetailView可以覆盖各种方法,例如get_queryset(),get_object()和get_context_data()
我get_context_data在阅读'object'属性时遇到了困难.特别是它似乎存在,但是类'对象':
def get_context_data(self, **kwargs):
context = super(SectionTextDetailView, self).get_context_data(**kwargs)
if object:
print (str(object))
## in reality try some logic with the object here
else:
print("very bad!")
return context
Run Code Online (Sandbox Code Playgroud)
(这打印"")
我怀疑get_object()之后被称为get_context_data()?真的吗?
我的整体问题是 - 类中方法的生命周期(评估顺序)是什么View,特别是DetailView?
我看着的参考条目DetailView和SingleObjectMixinDjango的文档中,它似乎并没有明确提到这一点.
我有一个after_initialize方法,如果我没有更新记录,我只想调用它.例如,类似以下内容:
after_initialize :set_some_name, :unless => :updating?
Run Code Online (Sandbox Code Playgroud)
例如,Task.find(123).title应该调用该after_initialize方法.Task.find(123).save!(:title => "something")应该不是调用after_initialize方法.
当然是"更新?" Rails中不存在方法.是否有相同的方法或方法?
在点击任何一个难度按钮后,我试图处理难度窗口,但不会发生.我试过了.dispose,frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);但我无法得到它.它只是放置还是更多?
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
public class Game extends JFrame{
public static JFrame frame = new JFrame();
private JLabel lab;
public static void main(String[] args) {
Game difficulty = new Game();
difficulty.setSize(350,105);
difficulty.setTitle("Difficulty.");
difficulty.setVisible(true);
difficulty.setLocationRelativeTo(null);
/**Game sudoku = new Game();
sudoku.setSize(900, 900);
sudoku.setVisible(false);*/
}
public Game(){
setLayout(new FlowLayout());
lab = new JLabel("Please select your difficulty.");
add(lab);
JButton easy; …Run Code Online (Sandbox Code Playgroud) 我正在使用php将csv文件导入我的数据库(Mysql).下面的代码只将第一行(csv文件中的数据)插入到数据库中.但它应该在数据库中插入多行.
$info = pathinfo($_FILES['file']['name']);
/**
* This checks if the file is a csv file
*/
if(strtolower($info['extension']) == 'csv'){
$filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0){
//Open the file
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE){
$num = count($emapData);
/**
* Insert data into the database if the columns are
* exactly in three columns
*/
if ($num == 3){
$sql = "INSERT
INTO Users (firstName,surName,indexNo)
VALUES('$emapData[0]','$emapData[1]','$emapData[2]')";
$result=$db->query($sql);
if($result){
echo "<script type=\"text/javascript\">
alert(\"CSV File has been successfully Imported.\"); …Run Code Online (Sandbox Code Playgroud) 在这种情况下 n=100
这是我的数据集
id amount
1 1000
2 2000
3 2300.7632
4 4560
Run Code Online (Sandbox Code Playgroud)
我想要的是
id amount
3 2300.7632
4 4560
Run Code Online (Sandbox Code Playgroud)