These are chat archives for django/django

19th
Jan 2016
friendOfOurs
@friendOfOurs
Jan 19 2016 14:59
what would be the motivation to have a model's field both in the modelForm's meta and as a class variable? e.g.
class SomeForm(models.ModelForm):
    email = forms.EmailField(required=True)
    class Meta:
        model=User,
        fields=['email', 'field1', 'field2']
Filip Figiel
@megapctr
Jan 19 2016 14:59
@friendOfOurs probably to provide additional kwargs to the field
friendOfOurs
@friendOfOurs
Jan 19 2016 15:23
@megapctr such as?
Filip Figiel
@megapctr
Jan 19 2016 15:24
anything. widget, label, help_text, etc
friendOfOurs
@friendOfOurs
Jan 19 2016 15:25
but all of those can be provided in Meta?
afaik
if not all than it makes sense to have class var with kwargs that are not provided in Meta, but which construct has precedence?
Filip Figiel
@megapctr
Jan 19 2016 15:26
in your example, if User.email is an EmailField(blank=False), then it's not needed to specify that email field on ModelForm
friendOfOurs
@friendOfOurs
Jan 19 2016 15:27
oh! but if User.email is EmailField(blank=True) than in the ModelForm the email has to be defined in both Meta and as class var?
Filip Figiel
@megapctr
Jan 19 2016 15:28
Meta.fields is another story - it declares which fields the form will effectively have
you may have lots of fields on your form, but only few in Meta.fields. In such case, only theses few fields would be rendered and validated
friendOfOurs
@friendOfOurs
Jan 19 2016 15:31
and the ones that are not in Meta.fields i will have to handle by myself? I thought that Meta.fields are somehow directly related to models fields, hence the question why to have both
Filip Figiel
@megapctr
Jan 19 2016 15:32
The fields not in Meta.fields would be effectively nonexistent, as your form would ignore them
In general, it's a good practice to declare Meta.fields explicitly, because in the future you may add a model field that you don't want to be editable by end-users
Filip Figiel
@megapctr
Jan 19 2016 15:38
@friendOfOurs If you don't declare a modelform field explicitly, it will be generated automatically from the model field
@friendOfOurs Meta.fields, on the other hand, is just for declaring which fields to use
ikhahmedov
@ikhahmedov
Jan 19 2016 16:24

Hi all! (@tomchristie) I have a question related to django rest framework and csrf, I want to implement signup using REST API, but every time I use browser based API testers (POSTMAN) I get CSRF error, but if I do it using curl, then everything is fine, can someone help me understand this issue, I have opened the code for csrf middleware, but haven't fully understand logic behind it, the most annoying thing is

# Wait until request.META["CSRF_COOKIE"] has been manipulated before
# bailing out, so that get_token still works
if getattr(callback, 'csrf_exempt', False):
   return None

this one sometimes return True (when used curl) sometimes return False (when used browser based API)

lego
@legobillyjoe
Jan 19 2016 20:28
This message was deleted
This message was deleted
This message was deleted
This message was deleted
lego
@legobillyjoe
Jan 19 2016 20:34

Hi, People! I am doing Django Tutorial.

https://docs.djangoproject.com/en/1.9/intro/tutorial04/#write-a-simple-form

I am stuck here. This is detail.html template code:

<h1>{{ question.question_text }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
    <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>

I don't understand these things:

  • {% if error_message %}
  • {{ forloop.counter }}
  • in question.choice_set.all value choice_set.all. Where does it come from?

The separete steps of the tutorial look clear, but then appear some peaces of code and clarity of what I am doing disappears.

Thanks in advance.

Mauro Oviedo
@moviedo
Jan 19 2016 20:47
@legobillyjoe {% %} {{ }} and is django templating https://docs.djangoproject.com/en/1.9/topics/templates/
choice_set and question are context variables that hold data, usually use something like
def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] context = {'latest_question_list': latest_question_list} return render(request, 'polls/index.html', context)