Domanda

Ho appena iniziato a lavorare con Django proveniente da anni di primavera MVC e le forme implementazioni sono leggermente pazze. Se non hai familiarità, Forme di django Inizia con una classe modello di modulo che definisce i tuoi campi. La primavera inizia allo stesso modo con un oggetto di back-backing. Ma dove la primavera fornisce un file taglib Per elementi di forma vincolante all'oggetto di supporto all'interno del tuo JSP, Django ha widget di forma legati direttamente al modello. Esistono widget predefiniti in cui è possibile aggiungere attributi di stile ai tuoi campi per applicare CSS o definire widget completamente personalizzati come nuove classi. Tutto va nel tuo codice Python. Mi sembra noci. Innanzitutto, stai mettendo informazioni sulla tua vista direttamente nel tuo modello e in secondo luogo stai legando il tuo modello a una vista specifica. Mi sto perdendo qualcosa?

Modifica: un codice di esempio come richiesto.

Django:

# Class defines the data associated with this form
class CommentForm(forms.Form):
    # name is CharField and the argument tells Django to use a <input type="text">
    # and add the CSS class "special" as an attribute. The kind of thing that should
    # go in a template
    name = forms.CharField(
                widget=forms.TextInput(attrs={'class':'special'}))
    url = forms.URLField()
    # Again, comment is <input type="text" size="40" /> even though input box size
    # is a visual design constraint and not tied to the data model
    comment = forms.CharField(
               widget=forms.TextInput(attrs={'size':'40'}))

Spring MVC:

public class User {
    // Form class in this case is a POJO, passed to the template in the controller
    private String firstName;
    private String lastName;
    get/setWhatever() {}
}

<!-- JSP code references an instance of type User with custom tags -->
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!-- "user" is the name assigned to a User instance -->
<form:form commandName="user">
      <table>
          <tr>
              <td>First Name:</td>
              <!-- "path" attribute sets the name field and binds to object on backend -->
              <td><form:input path="firstName" class="special" /></td>
          </tr>
          <tr>
              <td>Last Name:</td>
              <td><form:input path="lastName" size="40" /></td>
          </tr>
          <tr>
              <td colspan="2">
                  <input type="submit" value="Save Changes" />
              </td>
          </tr>
      </table>
  </form:form>

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
scroll top