ferrocarrilFormulario() missing 1 required positional argument: 'request' - python

I need help with my code, I'm making a form in django and I can't solve this error.
views.py:
def ferrocarrilFormulario(request):
if request.method =="POST":
miFormulario = ferrocarrilFormulario(request.POST)
print(miFormulario)
if miFormulario.is_valid:
informacion = miFormulario.cleaned_data
ferrocarril = ferrocarril(request.POST["tipo"], request.POST["precio"])
ferrocarril.save()
return render (request, "AppCoder/inicio.html")
Forms.py:
from django import forms
class ferrocarrilFormulario(forms.Form):
tipo= forms.CharField()
precio = forms.IntegerField()
Form HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Agrega un ferrocarril</title>
</head>
<body>
{% if.miFormulario.errors %}
<p style="color: red;"> Datos mal ingresados</p>
{% endif %}
<form action="" method="POST"{% csrf_token %}
<table>
{{ miFormulario.as_tabble }}
</table>
<input type="submit" value="Enviar">
></form>
</body>
</html>
urls.py:
from django.urls import path
from AppCoder import views
urlpatterns = [
path('',views.inicio, name="Inicio"),
path("ferrocarril/", views.ferrocarril, name="ferrocarril"),
path("vias/", views.vias, name="vias"),
path("manodeobra/", views.manodeobra, name="manodeobra"),
path("tables.html/", views.tables, name="tables"),
path("ferrocarrilFormulario/", views.ferrocarrilFormulario, name="ferrocarrilFormulario")
thank you <3
I wanted the form to work after that, but that is not the case.
PS: if I put the request, it generates another error, and in the tutorial it appears without the request.
Thanks again.

Related

No Output from views.py file after getting POST from template file (Django)

I have been trying to get details filled in a form,back in a python file using django. Everything seems fine but the form details are not getting displayed in the terminal.
views.py file:
from django.shortcuts import render
from basicapp import forms
# Create your views here.
def index(request):
return render(request,'basicapp/index.html')
def form_name_view(request):
form = forms.FormName()
if request == "POST":
form = forms.FormName(request.POST)
if form.is_valid():
#DO SOMETHING
print("Validation SUCCESS")
print("NAME: " + form.cleaned_data['name'])
print("EMAIL: " + form.cleaned_data['email'])
print("AVANODA KARUTHTHU: " + form.cleaned_data['text'])
return render(request,'basicapp/form_page.html', {'form' : form } )
form_page.html file : ( I have removed the Bootstrap CDNs )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Forms</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, shrink-to-fit=no">
</head>
<body>
<div class="container">
<div class = "jumbotron">
<h2> Fill out the form </h2>
<form method="POST" action = "">
{{ form.as_p }}
{% csrf_token %}
<input type="submit" class = "btn btn-primary" value = "Submit">
</form>
</div>
</div>
</body>
</html>
Please help. Thanks in advance .
Correction:
if request.method == "POST": under index function
UPDATE: FOUND THE SOLUTION. Please Refer to the attached image.

not able to upload and display django form

1.views.py
from django.shortcuts import render
from . models import User
from .forms import Userform
def Home(request):
if request.method == "POST":
form = Userform(request.POST or None,request.FILES or None)
if form.is_valid():
form.save()
else:
form = Userform()
return render(request,"home.html",{"form":form})
def read(request):
read = User.objects.all()
return render(request,"read.html",{"read":read})
2.models.py
from django.db import models
class User(models.Model):
name = models.CharField(max_length=12)
rollno = models.IntegerField()
# files = models.FileField()
3.form.py
from django import forms
from .models import User
class Userform(forms.ModelForm):
class Meta:
model = User
fields = ('name','rollno')
urls.py
from django.contrib import admin
from django.urls import path
from app import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path("",views.Home,name="Home"),
path("read/",views.read, name="read"),
]
urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
4.home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form method="POST" action="read/" enctype="multipart/form-data">
{%csrf_token%}
{{form}}
<button type=submit>click to submit</button>
</form>
</body>
</html>
read.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{%block containt%}
{%for i in read%}
{{i.name}}
{{i.rollno}}
{%endfor%}
{%endblock%}
</body>
</html>
i want to add data to django admin using a form but data is not uploading at django admin
Not able to upload data into django admin hence cant read it
please help
i want to add data to django admin using a form but data is not uploading at django admin
Not able to upload data into django admin hence cant read it
please help
Problem is in the action of your form, you have entered the wrong URL in the action
You have to put home URL instead of read/, because you are handling your form submission in your home function so replace your form action by this
<form method="POST" action="{% url 'Home' %}" enctype="multipart/form-data">
{%csrf_token%}
{{form}}
<button type=submit>click to submit</button>
</form>

why request.GET.get() returns 'None' and value of html element is not visible in URL

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HELLO</title>
</head>
<body>
<form action="/removepunc", method="get">
<input type="text",name='text' value="Hello,Django" />
<input type="submit">
</form>
</body>
</html>
views.py
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return render(request,'index.html')
def removepunc(request):
print("Text is :"+request.GET.get('text','default'))
return HttpResponse("Hello")
urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index,name='index'),
path('removepunc',views.removepunc,name='rempunc')
]
This is first screen after run the code
When I click on submit in Url it did not show "hello django"
Also in terminal it print default not "hello django"
There is a comma in your <input> box between "text" and name.
The <input> tag should thus look like:
<input type="text" name="text" value="Hello,Django" />
not:
<input type="text",name='text' value="Hello,Django" />

How do I fix '... is not a registered namespace' Django 2.1 error

so Im building and simple mooc plataform using Django 2.1 and Im having a problem.
Here you can find my entire project but ill describe all situation above, and here u can find a entire printscreen of django error.
So i have this project called TamerMooc, this project has 2 apps. A core app and a courses app.
This is the /courses/views.py file:
from django.shortcuts import render, get_object_or_404
from .models import Course
def index(request):
courses = Course.objects.all()
template_name = 'courses/index.html'
context = {
'courses':courses
}
return render(request, template_name, context)
def details(request, slug):
course = get_object_or_404(Course, slug=slug)
template_name = 'courses/details.html'
context = {
'course': course
}
return render(request, template_name, context)
And this is the /courses/urls.py file:
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('',views.index ,name='index'),
path('<slug:slug>', views.details , name='details'),
]
And at least the /core/templates/base.html file quoted in the error file.
<!doctype html>
{% load static %}
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Simple MOOC - Uma simples plataforma de ensino a distância" />
<title>Tâmer MOOC</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css">
<link rel="stylesheet" href="{% static 'css/styles.css' %}" />
</head>
<body>
<div class="header">
<div class="pure-menu pure-menu-open pure-menu-fixed pure-menu-horizontal">
<a class="pure-menu-heading" href="{% url 'home' %}">TÂMER MOOC</a>
<ul>
<li class="pure-menu-selected">Início</li>
<li>Cursos</li>
<li><a href="{% url 'contact' %}"}>Contato</a></li>
</ul>
</div>
</div>
<div class = "content">
{% block content %}{% endblock %}
<div class="footer">
Tâmer MOOC - Uma simples plataforma de ensino a distância
</div>
</div>
<script src="http://yui.yahooapis.com/3.12.0/build/yui/yui-min.js"></script>
</body>
</html>
I really read a lot of questions here on stackoverflow about similar errors but cant find a solution.
I think the problem is here.
<a href=" {{ course.get_absolute_url }} "> <!--- {% url 'courses:details' course.slug %}--->
^^^^^ ^^^^
In django template, comments does not work in this way. If you want to comment out something do it like this:
{% comment %}
{% url 'courses:details' course.slug %}
{% endcomment %}
Or
{# url 'courses:details' course.slug #}
Relevant documentation can be found here.

Displaying Audio/Video in Python Django Website

I am building a blog website where I can upload audio/video files to the database and then run a for loop to output the files in HTML audio/video tags. I have succeeded in saving the files to the database and also output them on the website but for some reason I can't get them to play.
I am still pretty new to Python/Django and I would appreciate it if anybody can help me figure this one out. Thanks in anticipation
Here is my models.py
from django.db import models
class Track(models.Model):
track_title = models.CharField(max_length=250)
track = models.FileField(upload_to='album/')
def __str__(self):
return self.track_title
Here is my index.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{% for track in tracks %}
<figure>
<figcaption>Audio</figcaption>
<audio controls>
<source src="{{ track.track.url }}" type="audio/mp3">
</audio>
</figure>
{% endfor %}
This is my views.py
from django.shortcuts import render
from .models import Track
def songView(request):
tracks = Track.objects.all()
context = {
'tracks':tracks
}
return render(request, 'album/index.html', context)
And my urls.py
from django.urls import path
from . import views
app_name = 'album'
urlpatterns = [
path('', views.songView, name='song'),
]

Categories

Resources