Django jQuery文件上传停止了更新

时间:2018-03-05 01:39:03

标签: django jquery-file-upload

我丢失了文件上传应用。我按照下面的网站进行了工作。

https://simpleisbetterthancomplex.com/tutorial/2016/11/22/django-multiple-file-upload-using-ajax.html

我不确定它是否正在更新到Django 2.0.2或jQuery文件上传的最新更新,但它现在无法正常工作。

当我检查表单是否有效时,这是我在视图中得到的错误。

<ul class="errorlist"><li>file<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

这是html:

<input id="fileupload" type="file" name="file" multiple style="display: none" data-url="{% url 'upload:index' %}" data-form-data='{"csrfmiddlewaretoken": "{{ csrf_token }}"}'>

是的,我更新了网址和表单。

型号:

class Upload ( models.Model ):
    title = models.CharField ( max_length = 255, blank = True )
    file = models.FileField ( upload_to = 'uploads/%Y/%m/%d/' )

形式:

class UploadForm ( forms.ModelForm ):

    class Meta:
        model = Upload
        fields = ['file']

感谢。

1 个答案:

答案 0 :(得分:0)

我不确定我是如何重新开始工作的,但是我做了一些改变,它再次起作用。

我认为是在视图中添加了form_class - 不知道为什么它会改变什么?

public class myTest {

  private WebDriver driver;

  //This codes in BeforeTest below will initiate a new chrome driver session 
  @BeforeTest
  public void initiate() {
    System.setProperty("webdriver.chrome.driver", driverpathchrome);
    DesiredCapabilities caps = DesiredCapabilities.chrome();
    LoggingPreferences logPrefs = new LoggingPreferences();
    logPrefs.enable(LogType.BROWSER, Level.ALL);
    caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
    driver = new ChromeDriver(caps);

    driver.manage().window().maximize();
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.get("www.test.com");
    log.info("Chrome browser launched. Going to " + "www.test.com");
  }


  private boolean isBrowserOpen() {
    try {
      driver.getTitle();
      return true;
    } catch (Exception e) {
      return false;
    }
  }

  public boolean amISeeingLoginScreenNow() {
    if (driver.findElements(By.cssSelector(".login-box.smaller")).size() != 0) {
      return true;
    } else {
      return false;
    }
  }

  //This is the test methods
  @Test(retryAnalyzer = myTest.RetryAnalyzer.class)
  private void loginToTestURL() throws Exception {

    //This will check if the current browser session is still open. If not, then create a new driver session
    if (this.isBrowserOpen() == false) {
      Thread.sleep(3000);

      // Initiating new Browser 
      this.initiate();
    } else {
      driver.get("www.test.com");
    }


    this.method_will_not_throw_exception() {
      //some codes that will work OK
    }


    try {
      this.method_will_throw_exception() {
        //some codes that will throw exception
        this.amISeeingLoginScreenNow(); //In the 2nd try , at this stage, Selenium will get grumpy
      }
    } catch (Exception e) {
      //put exception in the log files then re-throw the exception
      //retryAnalyzer will get the exception and then start the test from the start of @Test
      driver.quit();

      Thread.sleep(1000);
      this.initiate(); //re-initialise the new driver session before throwing exception to the retryAnalyzer

      throw new AssertionError();
    }

  }
}

希望这有帮助 - 虽然不问我为什么?

干杯。