Skip to content

Commit

Permalink
Merge pull request #1 from dabapps/readme-tweaks
Browse files Browse the repository at this point in the history
README tweaks
  • Loading branch information
j4mie authored Dec 16, 2021
2 parents 9054fd0 + 02f684f commit a1cfbdb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class SelectUserFromMyTeamForm(forms.Form):
team = kwargs.pop("team")
super().__init__(*args, **kwargs)
self.fields["user"].queryset = User.objects.filter(team=team)
```

```python
def select_user_view(request):
form = SelectUserFromMyTeamForm(team=request.user.team)
return render("form.html", {"form": form})
Expand All @@ -52,12 +54,15 @@ Here's how the code looks now:
```python
from dynamic_forms import DynamicField, DynamicFormMixin


class SelectUserFromMyTeamForm(DynamicFormMixin, forms.Form):
user = DynamicField(
forms.ModelChoiceField,
queryset=lambda form: User.objects.filter(team=form.context["team"]),
)
```

```python
def select_user_view(request):
form = SelectUserFromMyTeamForm(context={"team": request.user.team})
return render("form.html", {"form": form})
Expand Down Expand Up @@ -126,6 +131,7 @@ def htmx_form(request):
form = MakeAndModelForm()
return render(request, "htmx.html", {"form": form})


def htmx_models(request):
form = MakeAndModelForm(request.GET)
return HttpResponse(form["model"])
Expand Down Expand Up @@ -181,13 +187,15 @@ The form is exactly the same as the HTMX example above. But this time, we only n
def unpoly_form(request):
form = MakeAndModelForm(request.POST or None)
return render(request, "unpoly.html", {"form": form})
```

```python
urlpatterns = [
path("unpoly-form/", unpoly_form),
]
```

And the template is super simple:
And the template is even more simple:

```django
{% load widget_tweaks %}
Expand Down Expand Up @@ -247,7 +255,7 @@ class CancellationReasonForm(DynamicFormMixin, forms.Form):
forms.CharField,
include=lambda form: form["cancellation_reason"].value() == "other",
widget=lambda _: forms.TextArea,
)
)
```

## Why the awkward name?
Expand Down
2 changes: 2 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
INSTALLED_APPS = [
"tests",
]

SECRET_KEY = "abcde12345"

0 comments on commit a1cfbdb

Please sign in to comment.