I have a form that requires a URL as input. I would like to check if the URL begins with http:// or https://.
If it doesn't have one of these two 'stings' in the beginning, the form submission should give an error.
I don't have any clue on how to get started with this and could not find any info based on my limited knowledge of django and I have no clue what search terms to look up.
A basic hint would be a great help.
Thanks!
My current forms.py has a form based on a model:
class AddUrlForm(forms.ModelForm):
class Meta:
model = forwards
# fields = '__all__'
exclude = ["user", "counterA", "counterB", "shortcodeurl", "uniqueid"]
models.py:
class forwards(models.Model):
uniqueid = models.AutoField(primary_key=True)
user = models.CharField(max_length = 150)
urlA = models.CharField(verbose_name="URL Version A", max_length = 254)
counterA = models.DecimalField( max_digits=19, decimal_places=0,default=Decimal('0'))
urlB = models.CharField(verbose_name="URL Version B",max_length = 254)
counterB = models.DecimalField( max_digits=19, decimal_places=0,default=Decimal('0'))
timestamp = models.DateTimeField('date created', auto_now_add=True)
shortcodeurl = models.CharField(max_length = 254)
html segment where that shows how the form is integrated:
<form method="post">
{% csrf_token %}
{% for field in forwardform %}
<span>{{ field.label_tag }} </span>
<p style="color: black">{{ field }} </p>
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
{% endfor %}
<button class="btn btn-outline btn-xl type="submit">Generate URL</button>
</form>
views.py:
def forwardthis(request):
forwardform = AddUrlForm(request.POST or None)
if forwardform.is_valid():
forward = forwardform.save(commit=False)
forward.user = request.user.username
forward = forwardform.save()
uniqueid_local = forward.uniqueid
uniqueid_local_bytes = uniqueid_local.to_bytes((uniqueid_local.bit_length() + 7) // 8, byteorder='little')
shortcodeurl_local = urlsafe_base64_encode(uniqueid_local_bytes)
forward.shortcodeurl = shortcodeurl_local
forward.save()
return HttpResponseRedirect('/forwardthis')
query_results = forwards.objects.filter(user=request.user.username)
query_results_qty = query_results.count()
click_results = clickstats.objects.filter(user=request.user.username)
template = loader.get_template('forwardthis.html')
context = {
'forwardform': forwardform ,
'query_results':query_results,
'query_results_qty': query_results_qty
}
return HttpResponse(template.render(context,request))
You can create a validation method for each form field. def clean_FIELDNAME(). I supose the url field is shortcodeurl:
class AddUrlForm(forms.ModelForm):
def clean_shortcodeurl(self):
cleaned_data = self.clean()
url = cleaned_data.get('shortcodeurl')
if not is_valid_url(url): # You create this function
self.add_error('shortcodeurl', "The url is not valid")
return url
class Meta:
model = forwards
# fields = '__all__'
exclude = ["user", "counterA", "counterB", "shortcodeurl", "uniqueid"]
For anyone coming here in 2021.
Nowadays Django provides the tools to achieve this kind of validation.
Based on Django 3.2 documentation
from django import forms
from django.core.exceptions import ValidationError
class AddUrlForm(forms.ModelForm):
class Meta:
model = forwards
# fields = '__all__'
exclude = ["user", "counterA", "counterB", "shortcodeurl", "uniqueid"]
def clean_shortcodeurl(self):
data = self.cleaned_data['shortcodeurl']
if "my_custom_example_url" not in data:
raise ValidationError("my_custom_example_url has to be in the provided data.")
return data
Related
I have a simple project that has two different models. One that handles a report and one that stores the images for each report connected by a ForeignKey:
class Report(models.Model):
report_location = models.ForeignKey(Locations, on_delete=models.CASCADE)
timesheet = models.ImageField(upload_to='report_images', default='default.png')
text = models.CharField(max_length=999)
report_date = models.DateField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return f"{self.report_location, self.report_date, self.created_by}"
class TimeSheetAndPics(models.Model):
report = models.ForeignKey(Report, on_delete=models.CASCADE)
report_images = models.ImageField(upload_to='report_images', default='default.png')
date = models.DateField(auto_now=True)
def __str__(self):
return f"{self.report} on {self.date}"
My Goal is to have a user fill out the report and then upload multiple pictures, however i cannot figure out how to handle multiple image uploads.
I have two forms for each model:
class ReportCreationForm(ModelForm):
class Meta:
model = Report
fields = [
'report_location',
'text',
]
class TimeSheetAndPicForm(ModelForm):
report_images = forms.FileField(widget=ClearableFileInput(attrs={'multiple': True}))
class Meta:
model = TimeSheetAndPics
fields = [
'report_images',
]
And this is how i try to handle my views:
class NewReport(LoginRequiredMixin, View):
def get(self, request):
context = {
'create_form': ReportCreationForm(),
'image_form': TimeSheetAndPicForm(),
}
return render(request, 'rakan/report_form.html', context)
def post(self, request):
post = request.POST
data = request.FILES or None
create_form = ReportCreationForm(post)
image_form = TimeSheetAndPicForm(post, data)
if create_form.is_valid() and image_form.is_valid():
clean_form = create_form.save(commit=False)
clean_form.created_by = request.user
clean_form.save()
clean_image_form = image_form.save(commit=False)
for images in clean_image_form:
clean_image_form.report = clean_form
clean_image_form.report = images
clean_image_form.save()
return redirect('rakan:rakan_index')
return redirect('rakan:new-report')
I have tried to solve this in different ways but i unfortunately hit a wall. I cant seem to find a solution that actually works. My best try i was able to save only 1 image in the models instead of the 3 test images.
I dont believe it makes a difference but here is also the HTML File that uses the forms:
<div class="content-section">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Create Report</legend>
{{ create_form }}
<br>
{{ image_form }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">submit</button>
</div>
</form>
</div>
Anyone dealt with this problem would like to help me achieve a solution i would be very thankful. Thank you.
I want my django DateTimeField to be inputted the same way I input the values from the admin page, where the dates are selected from a calender.
for reference this is how my models.py look like:
from django.db import models
# Create your models here.
class TheDate(models.Model):
"""A topic the user is learning about"""
theDate = models.DateTimeField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""returns a string representation of the model"""
return str(self.theDate)
forms.py
class NewDate(forms.ModelForm):
class Meta:
model = TheDate
fields = ['theDate']
labels = {'theDate': ''}
the html page where i have set to create a new plan- new_date.html:
{% extends "meal_plans/base.html" %}
{% block content %}
<p>Add new Date:</p>
<form action="{% url 'meal_plans:new_date' %}">
{% csrf_token %}
{{ form.as_p }}
<button name="submit">Add New Date</button>
</form>
{% endblock content %}
what can i do to the data be inputted in that way
i hope the code here is enough and relevant.
Hello I had similar problem and I found something like this
class DateInput(forms.DateInput):
input_type = 'date'
And then in your modelform
class NewDate(forms.ModelForm):
class Meta:
model = TheDate
widgets = {
'theDate': DateInput()
}
fields = ['theDate']
Put in your forms.py something like this:
class NewDate(forms.ModelForm):
theDate = forms.DateTimeField (
widget=forms.DateTimeInput (
attrs={
'type': 'datetime-local',
}
)
)
class Meta:
model = TheDate
fields = ['theDate']
labels = {'theDate': ''}
This worked here!
I am trying to create a basic personality test in Django as a proof-of-concept at work. I'm new to Django (and python in general), coming at it from a C# .NET background.
I am trying to make a list of form objects (populated with information pulled from question objects stored in the database), then display them in the HTML.
This is only partly working; I can render the form attributes individually in a for loop (by calling, for example, question.pk) but nothing renders with the standard Django {{ form }} tag, and trying to submit the list of forms breaks the whole thing.
I'm pretty sure it's an issue with handling a bunch of form objects populated inside one larger html , but I'm not sure how to go about resolving it.
I've done some research into formsets, but I can't find any way to pre-populate the form items with information from the database.
Thanks in advance!
DISCQuestionForm in forms.py:
class DISCQuestionForm(forms.Form):
# create new form object from database question object
def __init__(
self,
pk,
disc_query,
dom_answer,
infl_answer,
stead_answer,
con_answer,
):
super().__init__()
self.pk = pk
self.disc_query = disc_query
self.dom_answer = dom_answer
self.infl_answer = infl_answer
self.stead_answer = stead_answer
self.con_answer = con_answer
self.disc_response = forms.DecimalField(
max_value=4,
widget=forms.NumberInput
)
disc_create method in views.py
# Create a new DISC assessment for current user
def disc_create(request, pk):
profile = User.objects.get(pk=pk)
user = int(profile.pk)
name = profile.name
rawquestionset = DISCQuestion.objects.all()
discformset = []
for item in rawquestionset:
question = DISCQuestionForm(
pk=item.pk,
disc_query=item.disc_query,
dom_answer=item.dom_answer,
infl_answer=item.infl_answer,
stead_answer=item.stead_answer,
con_answer=item.con_answer,
)
discformset.append(question)
if request.method == 'POST':
questionset = discformset[request.POST]
if questionset.is_valid():
dom = 0
infl = 0
stead = 0
con = 0
for discquestion in questionset:
if discquestion.disc_response == discquestion.dom_answer:
dom += 1
if discquestion.disc_response == discquestion.infl_answer:
infl += 1
if discquestion.disc_response == discquestion.stead_answer:
stead += 1
if discquestion.disc_response == discquestion.con_answer:
con += 1
disctest = DISCTest(
user=user,
name=name,
dom=dom,
infl=infl,
stead=stead,
con=con,
)
disctest.save()
else:
questionset = discformset
context = {
"pk": user,
"name": name,
"discquestionset": questionset
}
return render(request, "disc_create.html", context)
DISCTest and DISCQuestion models in models.py:
class DISCTest(models.Model):
user = models.ForeignKey('User', on_delete=models.CASCADE)
name = user.name
created_on = models.DateTimeField(auto_now_add=True)
dom = models.DecimalField(max_digits=3, decimal_places=0)
infl = models.DecimalField(max_digits=3, decimal_places=0)
stead = models.DecimalField(max_digits=3, decimal_places=0)
con = models.DecimalField(max_digits=3, decimal_places=0)
class DISCQuestion(models.Model):
disc_query = models.TextField()
disc_response = models.DecimalField(max_digits=1, decimal_places=0, null=True)
dom_answer = models.DecimalField(max_digits=1, decimal_places=0)
infl_answer = models.DecimalField(max_digits=1, decimal_places=0)
stead_answer = models.DecimalField(max_digits=1, decimal_places=0)
con_answer = models.DecimalField(max_digits=1, decimal_places=0)
and finally disc_create.html in templates:
{% extends "base.html" %}
{% block page_content %}
<div class="col-md-8 offset-md-2">
<h1>Take your DISC assessment</h1>
<hr>
<h3>Insert instructions here</h3>
<hr>
<form action="/assessment/create/{{pk}}/" method="post">
{% csrf_token %}
<div>
{% for question in discquestionset %}
<p>{{question.pk}}</p>
<p>{{ question.disc_query }}</p>
{{ form }}
{% endfor %}
</div>
<button type="submit">Submit</button>
</form>
</div>
{% endblock %}
Your DiscQuestionForm has no fields. disc_response is defined as an attribute of the form but for Django it isn't a field because it isn't added to self.fields. And form isn't defined in your template in your for loop, only question (which is the form) so {{ question }} would print the form if it had any fields.
But then the problem is that each of your question form fields would all have the same "name" attributes because they are not prefixed to make them unique.
You should read this document carefully to understand ModelForm and modelformset. Basically you need:
class DISCQuestionForm(forms.ModelForm):
class Meta:
model = DISCQuestion
def __init__(...):
...
Use the modelformset_factory to create a proper ModelFormSet that you can initialise with the request.POST when submitted.
DISCQuestionFormSet = modelformset_factory(DISCQuestionForm, form = DISCQuestionForm) # note DISCQuestionForm not needed if you don't customise anything in your form.
and in your view:
formset = DISCQuestFormSet(request.POST or None)
then in your template you can loop through the forms in the formset:
{% for form in formset %}{{ form }}{% endfor %}
I've been working on a small django website as I learn django. According to the documentation when you create a form class with a meta class that points at a model with foreign key fields, it'll render those fields as select inputs.
In my application I have 3 models, client test, and record where record carries two foreign keys, each of whom point to client and test respectively
Models.py
class Client(models.Model):
first = models.CharField(max_length=264)
last = models.CharField(max_length=264)
DOB = models.DateField()
def __str__(self):
return self.first + " " + self.last
class Test(models.Model):
test = models.CharField(max_length=264)
fee = models.DecimalField(max_digits=12, decimal_places=2)
def __str__(self):
return self.test
class Record(models.Model):
client = models.ForeignKey(Client, on_delete=models.CASCADE)
test = models.ForeignKey(Test, on_delete=models.CASCADE)
date = models.DateField()
def __str__(self):
return str(self.date) + " " + str(self.test) + " for " + str(self.client)
Form.py
class NewLabRecord(forms.ModelForm):
client = forms.ChoiceField(
label='Client ID',
widget=forms.Select(
attrs={'class': 'form-control'}))
test = forms.ChoiceField(
label='Test ID',
widget=forms.Select(
attrs={'class': 'form-control'}))
date = forms.DateField(
label='Test Date',
widget=forms.DateInput(
attrs={'class': 'form-control'}))
class Meta:
model = models.Record
fields = '__all__'
I render NewLabRecord at the top of my index view for records. The idea is to create a record and redirect back to the page (therefore seeing it in the list of records). Presently, I'm emulating class-based-views and not actually implementing it because I have not learned it yet. Nevertheless, this pattern does work for my client and test (the code is nearly identical).
Views.py
class LabRecord:
#staticmethod
def index(request):
new_record_form = forms.NewLabRecord
records = models.Record.objects.order_by('date')
print(records)
context = {
'records': records,
'new_record_form': new_record_form
}
return render(request, "layouts/lab/record/index.html", context=context)
layouts/lab/record/index.html
<div class="collapse" id="createLabRecord">
{% include 'components/lab/record/create.html' %}
</div>
components/lab/record/create.html
<!DOCTYPE html>
{% block content %}
<div class="card col-sm" style="">
<form class="form" method="post" action="{% url 'lab:create lab record' %}">
{{ new_record_form }}
{% csrf_token %}
<input type="submit" value="submit">
</form>
</div>
{% endblock %}
Now, when I go to the url for this view, /lab/records/, the view renders two select fields and an input for the date; however, the select fields are empty!
Note: I have 9 clients and 4 tests in the database!
Why is the view generating empty select fields for the foreign key fields?
In your view, you need to query your Client and Test models and put those lists into your context to make them available to your form/template.
records = models.Record.objects.order_by('date')
clients = models.Client.objects.all()
tests = models.Test.objects.all()
context = {
'records': records,
'new_record_form': new_record_form,
'clients' : clients,
'tests' : tests,
}
I have not learned the forms portion of django yet to tell you if there is something else to connect the lists to the input select fields.
Edit:
It looks like the following in your form should accomplish the desired:
class NewLabRecord(forms.ModelForm):
client = forms.ModelChoiceField(models.Client.objects.all())
test = forms.ModelChoiceField(models.Test.objects.all())
date = forms.DateField(
label='Test Date',
widget=forms.DateInput(
attrs={'class': 'form-control'}))
class Meta:
model = models.Record
fields = '__all__'
And I don't think that the changes to your view are then necessary.
Hi bit of a beginner question about using django's modelchoicefield in a form I'm building.
I just need get django to display a drop down list of ingredients in a form. I've gotten to the point where the page renders but the form does not, I was getting errors before so I am kind of perplexed at the moment. I was hoping for some guidance.
Using python 2.7.6 and django 1.6.2. If I left anything out let me know.
Thanks!
Code is below:
views:
args = {}
#add csrf sercurity
args.update(csrf(request))
args['form'] = form
return render_to_response('newMeal.html', args)
form:
from django import forms
from models import meals, ingredients, recipe
class mealForm(forms.ModelForm):
breakfast = forms.ModelChoiceField(queryset=recipe.objects.all())
# Lunch = forms.ModelChoiceField(queryset=recipe.objects.all())
# Dinner = forms.ModelChoiceField(queryset=recipe.objects.all())
class Meta:
model = meals
fields = ('Breakfast','Lunch','Dinner','servingDate')
class recipeForm(forms.ModelForm):
class Meta:
model = recipe
fields = ('Name', 'Directions')
template:
{% extends "base.html" %}
{% block content %}
<p>New Meals go here!</p>
<form action="/meals/newmeal/" method="post">{% csrf_token %}
<table class="selection">
{{form.as_table}}
<tr><td colspan="2"><input type="submit" name="submit" value="Add Meal"></td></tr>
</table>
</form>
{% endblock %}
Model;
from django.db import models
import datetime
Create your models here.
class recipe(models.Model):
Name = models.CharField(max_length=200)
Directions = models.TextField()
pub_date = models.DateTimeField(auto_now_add = True)
def __unicode__(self):
return (self.id, self.Name)
class ingredients(models.Model):
Name = models.CharField(max_length=200)
Quantity = models.IntegerField(default=0)
Units = models.CharField(max_length=10)
Recipe = models.ForeignKey(recipe)
def __unicode__(self):
return self.Name
class meals(models.Model):
Breakfast = models.CharField(max_length=200)
Lunch = models.CharField(max_length=200)
Dinner = models.CharField(max_length=200)
servingDate = models.DateTimeField('date published')
did you import the mealForm:
some thing like :from app.forms import mealForm
form is a function. so try:
args['form'] = mealForm()
Note: don't use render_to_response. it is old use render instead(so don't even need csrf)::
from django.shortcuts import render
def...(request):
....
return render(request,'newMeal.html', {'form': mealForm()})